Python 如何将列添加到空的熊猫数据框中?

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

How to add columns to an empty pandas dataframe?

pythonpandasdataframe

提问by 00__00__00

I have an empty dataframe.

我有一个空的dataframe.

df=pd.DataFrame(columns=['a'])

for some reason I want to generate df2, another empty dataframe, with two columns 'a' and 'b'.

出于某种原因,我想生成另一个空数据框 df2,其中包含两列“a”和“b”。

If I do

如果我做

df.columns=df.columns+'b'

it does not work (I get the columns renamed to 'ab') and neither does the following

它不起作用(我将列重命名为“ab”)并且以下内容也不起作用

df.columns=df.columns.tolist()+['b']

How to add a separate column 'b' to df, and df.emtpykeep on being True?

如何向 df 添加一个单独的列“b”,并df.emtpy继续保持True

Using .loc is also not possible

使用 .loc 也是不可能的

   df.loc[:,'b']=None

as it returns

当它返回时

  Cannot set dataframe with no defined index and a scalar

回答by Sumit Jha

Here are few ways to add an empty column to an empty dataframe:

以下是将空列添加到空数据帧的几种方法:

df=pd.DataFrame(columns=['a'])
df['b'] = None
df = df.assign(c=None)
df = df.assign(d=df['a'])
df['e'] = pd.Series(index=df.index)   
df = pd.concat([df,pd.DataFrame(columns=list('f'))])
print(df)

Output:

输出

Empty DataFrame
Columns: [a, b, c, d, e, f]
Index: []

I hope it helps.

我希望它有帮助。

回答by Ben.T

If you just do df['b'] = Nonethen df.emptyis still Trueand df is:

如果你只是这样做,df['b'] = None那么df.empty仍然True和 df 是:

Empty DataFrame
Columns: [a, b]
Index: []

EDIT: To create an empty df2from the columns of dfand adding new columns, you can do:

编辑:要从df2列中创建一个空df并添加新列,您可以执行以下操作:

df2 = pd.DataFrame(columns = df.columns.tolist() + ['b', 'c', 'd'])

回答by jpp

This is one way:

这是一种方式:

df2 = df.copy()
df2 = df2.join(pd.DataFrame(columns=['b']))

The advantage of this method is you can add an arbitrary number of columns without explicit loops.

这种方法的优点是您可以添加任意数量的列而无需显式循环。

In addition, this satisfies your requirement of df.emptyevaluating to Trueif no data exists.

此外,这也满足您df.empty评估True无数据时的要求。

回答by ALollz

If you want to add multiple columns at the same time you can also reindex.

如果您想同时添加多个列,您还可以重新索引。

new_cols = ['c', 'd', 'e', 'f', 'g']
df2 = df.reindex(df.columns.union(new_cols), axis=1)

#Empty DataFrame
#Columns: [a, c, d, e, f, g]
#Index: []

回答by YOBEN_S

You can using concathere

你可以concat在这里使用

df=pd.DataFrame(columns=['a'])
df
Out[568]: 
Empty DataFrame
Columns: [a]
Index: []

df2=pd.DataFrame(columns=['b', 'c', 'd'])
pd.concat([df,df2])
Out[571]: 
Empty DataFrame
Columns: [a, b, c, d]
Index: []