我有一个这样的熊猫数据框:
df1:
id name gender
1 Alice Male
2 Jenny Female
3 Bob Male
现在我想添加一个新的列体育项目,其中将包含列表形式的值。让我们将“足球”添加到性别为男性的行中,因此df1如下所示:
df1:
id name gender sport
1 Alice Male [Football]
2 Jenny Female NA
3 Bob Male [Football]
现在,如果我想将羽毛球添加到性别为女性的行中,将网球添加到性别为男性的行中,以便最终输出为:
df1:
id name gender sport
1 Alice Male [Football,Tennis]
2 Jenny Female [Badminton]
3 Bob Male [Football,Tennis]
如何在python中编写一个通用函数,该函数将完成基于其他某些列值将新值附加到该列的任务?
答案 0 :(得分:0)
以下内容应为您工作。用空列表初始化列并继续
df['sport'] = np.empty((len(df), 0)).tolist()
def append_sport(df, filter_df, sport):
df.loc[filter_df, 'sport'] = df.loc[filter_df, 'sport'].apply(lambda x: x.append(sport) or x)
return df
filter_df = (df.gender == 'Male')
df = append_sport(df, filter_df, 'Football')
df = append_sport(df, filter_df, 'Cricket')
输出
id name gender sport
0 1 Alice Male [Football, Cricket]
1 2 Jenny Female []
2 3 Bob Male [Football, Cricket]