pandas read_csv 删除空白行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52135459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:59:50  来源:igfitidea点击:

pandas read_csv remove blank rows

pythonpandascsvblank-line

提问by Karvy1

I am reading in a CSV file as a DataFramewhile 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?

我正在读取 CSV 文件,DataFrame同时定义每列的数据类型。如果 CSV 文件中有空行,则此代码会出错。如何在没有空白行的情况下读取 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

My CSV file's snapshot - enter image description here

我的 CSV 文件的快照 - 在此处输入图片说明

回答by fuwiak

try smth like this:

像这样尝试:

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

回答by NadavWeisler

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)

回答by Shivaansh Agarwal

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