用apply和lambda替换pandas iterrows()

时间:2018-12-19 01:27:57

标签: python pandas dataframe

使用iterrows()将新列添加到Pandas DataFrame的工作代码。但这非常慢

df1['Kite_Token'] =0

for row,col in df.iterrows():
    df1.loc[row, 'Kite_Token'] = df_instrument[(df_instrument.exchange_token==col.SC_CODE) & (df_instrument.exchange == 'BSE')].instrument_token.to_string(index=False)

这是我尝试对上述代码使用apply和lambda,但收到复制警告。有没有警告消息的更快的完成方式?

df1['Kite_Token']= df1['SC_CODE'].apply(lambda x : df_instrument[(df_instrument.exchange_token==x) & (df_instrument.exchange == 'BSE')].instrument_token.to_string(index=False))

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

0 个答案:

没有答案