pandas read_csv remove blank rows

大兔子大兔子 提交于 2020-07-09 07:37:55

问题


I am reading in a CSV file as a DataFrame while defining each column's data type. This code gives an error if the CSV file has a blank row in it. How do I read the CSV without blank rows?

dtype = {'material_id': object, 'location_id' : object, 'time_period_id' : int, 'demand' : int, 'sales_branch' : object, 'demand_type' : object }

df = pd.read_csv('./demand.csv', dtype = dtype)

I thought of one workaround of doing something like this but not sure if this is the efficient way:

df=pd.read_csv('demand.csv')
df=df.dropna()

and then redefining the column data types in the df.

Edit : Code -

import pandas as pd
dtype1 = {'material_id': object, 'location_id' : object, 'time_period_id' : int, 'demand' : int, 'sales_branch' : object, 'demand_type' : object }
df = pd.read_csv('./demand.csv', dtype = dtype1)
df

Error - ValueError: Integer column has NA values in column 2

My CSV file's snapshot -


回答1:


try smth like this:

data = pd.read_table(filenames,skip_blank_lines=True, a_filter=True)



回答2:


This worked for me.

def delete_empty_rows(file_path, new_file_path):
    data = pd.read_csv(file_path, skip_blank_lines=True)
    data.dropna(how="all", inplace=True)
    data.to_csv(new_file_path, header=True)



回答3:


solution could be :

data = pd.read_table(filenames,skip_blank_lines=True, na_filter=True)



回答4:


try.csv

s,v,h,h
1,2,3,4

4,5,6,7



9,10,1,2

Python Code

df = pd.read_csv('try.csv', delimiter=',')
print(df)

Output

   s   v  h  h.1
0  1   2  3    4
1  4   5  6    7
2  9  10  1    2


来源:https://stackoverflow.com/questions/52135459/pandas-read-csv-remove-blank-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!