![](/img/trans.png)
[英]Overwriting values in a pandas dataframe based on NA values from a second one
[英]Set values in Pandas one dataframe based on rows in second dataframe
我有兩個數據框 df1 和 df2,我想在 df1 中創建一個新列,並將該列中的值設置為 0,其中 df1 中的行包含在 df2 中。 進一步來說:
sample_data_1 = {'col1': ['80', '8080'], 'col2': ['0.0.0.0', '143.21.7.165']}
df1 = pd.DataFrame(data=sample_data_1)
sample_data_2 = {'col1': ['80', '8080', '1', '8888'], 'col2': ['0.0.0.0', '143.21.7.165', '1', '5.5.5.5'], 'col3': ['1','2','3']}
df2 = pd.DataFrame(data=sample_data_2)
col1 col2
0 80 0.0.0.0
1 8080 143.21.7.165
col1 col2 col3
0 80 0.0.0.0 1
1 8080 143.21.7.165 2
2 1 1 3
3 8888 5.5.5.5 4
我想向 df1 添加一列並將這些值設置為 0,其中 df1 中的 col1 和 col2 匹配 df2 中的 col1 和 col2。 結果 dataframe 應如下所示:
col1 col2 score
0 80 0.0.0.0 0
1 8080 143.21.7.165 0
當 dataframe 尺寸相同時,我可以使用.loc function 和邏輯與進行直接比較,但是當它們具有不同的形狀時,我會得到“無法比較系列”的異常。 想法?
謝謝您的幫助!
您可以使用df.merge
:
In [2735]: df1 = df1.merge(df2, on=['col1','col2']).drop('col3',1).assign(score=0)
In [2737]: df1
Out[2737]:
col1 col2 score
0 80 0.0.0.0 0
1 8080 143.21.7.165 0
如果 col1 中的條目不相同,則可以將 col1 設置為索引。 恰恰:
df = df2.set_index('col1').reindex(df1.set_index('col1').index)
df['score']=0
df.reset_index(inplace=True)
通過壓縮df1, df2
中的公共列來檢查成員資格 這將返回 boolean
使用np.where(condition, if condition, not condition)
,計算您想要的 output
import numpy as np
df1['score']=np.where([x in y for x,y in zip(df1.col1,df2.col1)],0,'not available')
col1 col2 score
0 80 0.0.0.0 0
1 8080 143.21.7.165 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.