附加到熊猫中的空DataFrame吗?

问题:附加到熊猫中的空DataFrame吗?

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

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

例如

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

结果看起来像这样:

Empty DataFrame
Columns: []
Index: []

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: []

回答 0

那应该工作:

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

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

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

That should work:

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

But the append doesn’t happen in-place, so you’ll have to store the output if you want it:

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

回答 1

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

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

这给你:

   age  height name
0    9       2  Zed

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

回答 2

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

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

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

You can concat the data in this way:

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

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