[英]pandas dataframe create new columns and fill with calculated values from same df
[英]pandas dataframe create new columns and fill with an external API response as calculated using concertinaed values from same df
df=
User id
0 u1 id1
1 u2 id2
2 u3 id3
user_limit1=api('u1:id1')
new_df=
User id user_limit
0 u1 id1 user_limit1
1 u2 id2 user_limit2
2 u3 id3 user_limit3
我怎样才能像上面那样为大约 9800 行 DF 更新 df?
创建列并在行上使用apply
df['user_limit'] = df.apply(lambda x: api_call(f'{x.User}:{x.id}'), axis=1)
或者
df['user_limit'] = df.User + ':' + df.id
df['user_limit'] = df.user_limit.map(api_call)
注意:确保api_call
function 只返回一个值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.