使用Pandas

时间:2017-07-18 14:25:14

标签: python pandas csv

我正在尝试使用Pandas从CSV文件导入数据集。问题是我在前几行的最后5列中有一些空单元格。这些细胞随后会被填充。如何仅导入整个数据集而不导入非空列?

1 个答案:

答案 0 :(得分:1)

空单元格应该只是csv文件中的一系列逗号。

#write a data frame to csv
pd.DataFrame({'A':[1, 2, 3],'B':[np.nan,4,5],'C':[np.nan,6,7] }).to_csv('/tmp/df.csv')

#I'm using iPython here, but however you want to view the df
!cat /tmp/df.csv 
,A,B,C
0,1,,
1,2,4.0,6.0
2,3,5.0,7.0

#read the df back in from CSV
pd.read_csv('/tmp/df.csv', index_col=0)
   A    B    C
0  1  NaN  NaN
1  2  4.0  6.0
2  3  5.0  7.0

查看CSV的第一行(非标题)行,您可以看到空白单元格用逗号表示,后面没有任何内容。

相关问题