pandas 熊猫空数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33487402/
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
Pandas empty dataframe
提问by Stefano Potter
I have a csv file which looks like this:
我有一个 csv 文件,它看起来像这样:
-0.08150654489363679, 0.3262445628643036, -0.1983973830938339, 0.04597456371557881
and I am reading the file in like this:
我正在像这样阅读文件:
import pandas as pd
df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv')
print df
which returns this:
返回这个:
Empty DataFrame
Columns: [-0.08150654489363679, 0.3262445628643036, -0.1983973830938339, 0.04597456371557881]
Index: []
I want to add column names to the columns like this:
我想将列名添加到这样的列中:
df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv')
df.columns=['Mean', 'Max', 'Min', 'Stdev']
print df
but when I do this I get this:
但是当我这样做时,我得到了这个:
Empty DataFrame
Columns: [Mean, Max, Min, Stdev]
Index: []
My desired output is this:
我想要的输出是这样的:
Mean Max Min Stdev
-0.08150654489363679 0.3262445628643036 -0.1983973830938339 0.04597456371557881
something funny is going on when the dataframe is being read but Im not sure what.
读取数据帧时发生了一些有趣的事情,但我不确定是什么。
采纳答案by EdChum
Pass the columns names as an arg to read_csv
:
将列名称作为 arg 传递给read_csv
:
df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv', names=['Mean', 'Max', 'Min', 'Stdev'])
by default it treats the first header row as the column names so when you overwrite the columns you end up with an empty df as you only had a single row in your csv in the first place.
默认情况下,它将第一个标题行视为列名,因此当您覆盖列时,您最终会得到一个空的 df,因为首先您的 csv 中只有一行。
Also it looks like your file has initial white space, you can set to skip these too:
此外,您的文件似乎有初始空白,您也可以设置跳过这些:
df=pd.read_csv(r'F:\Sheyenne\Statistics\IDL_stats\Basic_Stats\NDII\NDII_1984137_A_Annex.csv', names=['Mean', 'Max', 'Min', 'Stdev'], skipinitialspace=True)