1 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以創(chuàng)建一個(gè)列圖并應(yīng)用function內(nèi)部loop:
columns_map = (
('Grubhub', 'On GrubHub or Seamless?'),
('ToastTab', 'On ToastTab?'),
('Tenk', 'On Tenk?'),
# etc ...
)
for new_col, alias in columns_map:
df[new_col] = df[alias].apply(lambda x: x == 'Yes')
# also you can easily remove aliases columns:
# df = df.drop(columns=[alias])
或者您可以將值設(shè)置到原始列中并根據(jù)需要重命名(不帶drop()):
for new_col, alias in columns_map:
df[alias] = df[alias].apply(lambda x: x == 'Yes')
df.rename(
columns={alias: new_col for new_col, alias in columns_map},
inplace=True
)
添加回答
舉報(bào)