pandas read_csv删除空白行 [英] pandas read_csv remove blank rows
本文介绍了 pandas read_csv删除空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在定义每一列的数据类型时,我正在读取CSV文件作为DataFrame
.如果CSV文件中包含空白行,则此代码会产生错误.如何读取没有空白行的CSV?
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()
,然后在df
中重新定义列数据类型.
and then redefining the column data types in the df
.
代码-
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
错误-ValueError: Integer column has NA values in column 2
我的CSV文件的快照-
My CSV file's snapshot -
推荐答案
这对我有用.
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)
这篇关于 pandas read_csv删除空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文