简体   繁体   English

Append 列到 dataframe 使用列表理解格式

[英]Append column to a dataframe using list comprehension format

I would like to append a column of zeros to a dataframe if the column in question is not already inside the dataframe.如果有问题的列不在 dataframe 中,我想将 append 一列零设置为 dataframe。

If the dataframe looks like this:如果 dataframe 看起来像这样:

df = pd.DataFrame({'a':[0,1,0], 'c':[1,1,1]})

----------------------------------------------
    a   c
0   0   1
1   1   1
2   0   1

And the complete list of column names that the dataframe should have are: dataframe 应具有的完整列名列表是:

col_names = ['a', 'b', 'c']

I would like the output to look like this after applying the list comprehension to the df:在将列表理解应用于 df 后,我希望 output 看起来像这样:

    a    b    c
0   0    0    1
1   1    0    1
2   0    0    1


This is the complete code I have so far:这是我到目前为止的完整代码:

col_names = ['a','b','c']

df = pd.DataFrame({'a':[0,1,0], 'c':[1,1,1]})

# This is what I would like to convert into a list comprehension (one line) format if possible
for col in col_names:
    if col not in df.columns:
        df[col] = 0

# Re-order the columns into the correct order        
df = df[col_names]

print(df)

A list comprehension would produce a list.列表理解会产生一个列表。 You don't want a list, you want to add columns to your dataframe. List comprehensions should not be used for side effects, ever.您不需要列表,您想要向您的 dataframe 添加列。列表推导永远不应该用于副作用。

You can however, produce the columns you want to add as a list and use advanced indexing to assign all the columns at the same time:但是,您可以生成要添加为列表的列,并使用高级索引同时分配所有列:

df[[col for col in col_names if col not in df.columns]] = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM