python 如何使用pd.apply()来示例化新列?

z9smfwbn  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(137)

而不是这样做:

df['A'] = df['A'] if 'A' in df else None
df['B'] = df['B'] if 'B' in df else None
df['C'] = df['C'] if 'C' in df else None
df['D'] = df['D'] if 'D' in df else None
...

我想在一行或一个函数中完成这个操作。下面是我尝试的内容:

def populate_columns(df):
        
    col_names = ['A', 'B', 'C', 'D', 'E', 'F', ...]
               
    def populate_column(df, col_name):
        df[col_name] = df[col_name] if col_name in df else None
        return df[col_name]
        
    df[col_name] = df.apply(lambda x: populate_column(x) for x in col_names)    
    return df

但我刚得到Exception has occurred: ValueError,我在这里能做什么?

deikduxw

deikduxw1#

看起来您可以将整个代码替换为reindex

ensure_cols = ['A', 'B', 'C', 'D']
df = df.reindex(columns=df.columns.union(ensure_cols))
  • 注意:默认情况下填充值为NaN,如果确实需要None,请使用fill_value=None。*

如果您想修复代码,只需使用单个循环:

col_names = ['A', 'B', 'C', 'D']
for c in col_names:
    if c not in df:
        df[c] = None

相关问题