pandas 读取csv文件时擦除空白行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45346621/
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
Erase blank rows while reading csv file
提问by BEAst
I have tried to delete blank rows from my cvs file, however this is not working, it only writes out the first line
我试图从我的 cvs 文件中删除空白行,但这不起作用,它只写出第一行
please take a look and tell me how i can get all the rows with text and skip the rows that are blank
请看一看并告诉我如何获取所有带有文本的行并跳过空白行
Here is the code:
I just reads out the first line of the csv file
Thank you in advance!
先感谢您!
回答by Mohamed Thasin ah
First read your csv file with pandas with
首先用Pandas读取你的csv文件
df=pd.read_csv('input.csv')
then remove blank rows,
然后删除空白行,
df=df.dropna()
For more details in dropna, check the documentation.
有关 dropna 的更多详细信息,请查看文档。
回答by Bharath
If I have a csv file like below with blank row
如果我有一个像下面这样的空白行的 csv 文件
B;D;K;N;M;R 0;2017-04-27 01:35:30;C;3.5;A;01:15:00;23.0 1;2017-04-27 01:37:30;B;3.5;B;01:13:00;24.0 2;2017-04-27 01:39:00;K;3.5;C;00:02:00;99.0 4;2017-04-27 01:39:00;K;3.5;C;00:02:00;99.0
df = pd.read_csv('input.csv',delimiter=';')
will give the dataframe ignoring the blank lines.
df = pd.read_csv('input.csv',delimiter=';')
将使数据框忽略空行。
B D K N M R 0 2017-04-27 01:35:30 C 3.5 A 01:15:00 23.0 1 2017-04-27 01:37:30 B 3.5 B 01:13:00 24.0 2 2017-04-27 01:39:00 K 3.5 C 00:02:00 99.0 4 2017-04-27 01:39:00 K 3.5 C 00:02:00 99.0
Your code works when you use open
. Pandas read_csv will convert the csv file into dataframe. You might be confused with one another.
当您使用open
. Pandas read_csv 会将 csv 文件转换为数据帧。你可能会彼此混淆。
df = open('input.csv')
new_contents = []
for line in df:
if not line.strip():
continue
else:
new_contents.append(line)
回答by jezrael
There is problem:
有问题:
for line in df:
print (line)
return columns names.
返回列名。