Python 附加到 Pandas 中的空数据帧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16597265/
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-08-18 23:06:56  来源:igfitidea点击:

Appending to an empty DataFrame in Pandas?

pythonpandas

提问by ericmjl

Is it possible to append to an empty data frame that doesn't contain any indices or columns?

是否可以附加到不包含任何索引或列的空数据框?

I have tried to do this, but keep getting an empty dataframe at the end.

我试过这样做,但最后总是得到一个空的数据框。

e.g.

例如

df = pd.DataFrame()
data = ['some kind of data here' --> I have checked the type already, and it is a dataframe]
df.append(data)

The result looks like this:

结果如下所示:

Empty DataFrame
Columns: []
Index: []

采纳答案by DSM

That should work:

那应该工作:

>>> df = pd.DataFrame()
>>> data = pd.DataFrame({"A": range(3)})
>>> df.append(data)
   A
0  0
1  1
2  2

But the appenddoesn't happen in-place, so you'll have to store the output if you want it:

但是,append没有就地发生,所以你必须要存储输出,如果你想它:

>>> df
Empty DataFrame
Columns: []
Index: []
>>> df = df.append(data)
>>> df
   A
0  0
1  1
2  2

回答by dval

And if you want to add a row, you can use a dictionary:

如果你想添加一行,你可以使用字典:

df = pd.DataFrame()
df = df.append({'name': 'Zed', 'age': 9, 'height': 2}, ignore_index=True)

which gives you:

这给了你:

   age  height name
0    9       2  Zed

回答by Deepish

You can concat the data in this way:

您可以通过以下方式连接数据:

InfoDF = pd.DataFrame()
tempDF = pd.DataFrame(rows,columns=['id','min_date'])

InfoDF = pd.concat([InfoDF,tempDF])