[英]pandas read_csv remove blank rows
I am reading in a CSV file as a DataFrame
while defining each column's data type.我在定义每列的数据类型时将 CSV 文件作为
DataFrame
读取。 This code gives an error if the CSV file has a blank row in it.如果 CSV 文件中有空行,此代码会给出错误。 How do I read the CSV without blank rows?
如何在没有空白行的情况下读取 CSV?
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
.然后重新定义
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
错误 -
ValueError: Integer column has NA values in column 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)
像这样尝试:
data = pd.read_table(filenames,skip_blank_lines=True, a_filter=True)
解决方案可能是:
data = pd.read_table(filenames,skip_blank_lines=True, na_filter=True)
try.csv尝试.csv
s,v,h,h
1,2,3,4
4,5,6,7
9,10,1,2
Python Code Python代码
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
I am not sure whether its efficient or not but it works.我不确定它是否有效,但它有效。 This code does not load nan values while reading a csv.
此代码在读取 csv 时不会加载 nan 值。
df=pd.read_csv('demand.csv').dropna()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.