python pandas在一行代码中转换for循环

frebpwbc  于 2023-01-19  发布在  Python
关注(0)|答案(3)|浏览(207)

我有一个Dataframe df,您可以通过运行以下命令获得它:

import pandas as pd
  
data = [10,20,30,40,50,60]
  
df = pd.DataFrame(data, columns=['Numbers'])
  
df

现在我想检查df的列是否在现有列表中,如果不在则新建一列并将列值设置为0,列名与列表值相同:

columns_list=["3","5","8","9","12"]

for i in columns_list:
   if i not in df.columns.to_list():
        df[i]=0

我怎么能在一行代码,我已经尝试了这个:

[df[i]=0 for i in columns_list if i not in df.columns.to_list()]

但是IDE返回:

SyntaxError: cannot assign to subscript here. Maybe you meant '==' instead of '='?

有朋友能帮忙吗?

nukf8bse

nukf8bse1#

尝试:

columns_list=["3","5","8","9","12"]

df = df.reindex(
  set(
    list(df.columns) + columns_list
  ), 
  axis=1, 
  fill_value=0,
)
h7appiyu

h7appiyu2#

您还可以使用字典解包运算符。

columns_list = ["3","5","8","9","12"]
df = df.assign(**{col: 0 for col in columns_list if col not in df.columns})

使用df.assign,您可以解压缩所创建的字典,其中包含不属于columns_list的所有列,并为该列添加值0。
如果您真的希望将其放在一行中,那么也可以移动column_list

df = df.assign(**{col: 0 for col in ["3","5","8","9","12"] if col not in df.columns})
vd2z7a6w

vd2z7a6w3#

import numpy as np
import pandas as pd

# Some example data
df = pd.DataFrame(
    np.random.randint(10, size=(5, 6)),
    columns=map(str, range(6))
)

#    0  1  2  3  4  5
# 0  9  4  8  7  3  6
# 1  6  9  0  5  3  4
# 2  7  9  0  9  0  3
# 3  4  4  6  4  6  4
# 4  6  9  7  1  5  5

columns_list=["3","5","8","9","12"]

# Figure out which columns in your list do not appear in your dataframe
# by creating a new Index and using pd.Index.difference:
df[ pd.Index(columns_list).difference(df.columns, sort=False) ] = 0

#    0  1  2  3  4  5  8  9  12
# 0  9  4  8  7  3  6  0  0   0
# 1  6  9  0  5  3  4  0  0   0
# 2  7  9  0  9  0  3  0  0   0
# 3  4  4  6  4  6  4  0  0   0
# 4  6  9  7  1  5  5  0  0   0

相关问题