简体   繁体   English

如何基于共享公用密钥的另一个数据框的值“更新” df? 蟒蛇

[英]how do i 'update' a df based on the values of another dataframe that shares a common key? python

how do i 'update' a12 based on the values of another dataframe that shares a common key? 如何基于共享公用密钥的另一个数据框的值“更新” a12? In the example below, the common key is column a. 在下面的示例中,公用密钥为列a。

a12 = a12 =

  a  b  c
  0  1  1
  1  na na

try10 = try10 =

  a  b  c
  1  1  1

when i use a merge, I get something like this. 当我使用合并时,我得到这样的东西。

pd.merge(a12, try10) = pd.merge(a12,try10)=

  a  b  c  b_y  c_y
  0  1  1   na  na
  1  na na  1   1

I decided to do it manually, but i'm thinking there must be a more pythonic way than the below. 我决定手动执行此操作,但我认为必须有一种比以下方法更加Python化的方法。 I'd appreciate your help. 多谢您的协助。

for i, val in a12.iterrows():
    for x, xval in try10.iterrows():
        if xval['Firm1'] == val['Firm']: 
            try10.ix[x]['AMranking'] =  val['AMranking']
            try10.ix[x]['numlawyers'] = val['numlawyers']
            try10.ix[x]['grossprofits'] = val['grossprofits']  

del try12['firm']

There is an combine_first method that you can use. 有可以使用的combine_first方法。 See here. 看这里。 You'll need to set the desired key in both dataframes as index. 您需要在两个数据帧中将所需的键设置为索引。

In [128]: a12.set_index ('a').combine_first(try10.set_index('a'))
Out[128]: 
   b  c
a      
0  1  1
1  1  1

You can just apply dropna to a12 before merge : 您可以在merge之前merge dropna应用于a12

In [53]:

a12 = pd.DataFrame({'a':[0, 1], 'b':[1, np.nan], 'c':[1, np.nan]})
try10= pd.DataFrame({'a':[1], 'b':[1], 'c':[1]})
In [54]:

print pd.merge(a12, try10, how='outer', left_on='a', right_on='a')
   a  b_x  c_x  b_y  c_y
0  0    1    1  NaN  NaN
1  1  NaN  NaN    1    1
In [55]:

print pd.merge(a12.dropna(0), try10, how='outer')
   a  b  c
0  0  1  1
1  1  1  1

If a is the index: 如果a是索引:

In [57]:

print pd.merge(a12.dropna(0).reset_index(), try10.reset_index(), how='outer').set_index('a')
   b  c
a      
0  1  1
1  1  1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据多个列共有的值对数据框进行分组? - How do I groupby a dataframe based on values that are common to multiple columns? python - 如何将值从一个df传输到另一个 - python - how do I transfer values from one df to another 如何根据公共值将数据框中列的值添加到另一个数据框中? 熊猫/蟒蛇 - how to add values of a column in dataframe to another dataframe based on common values? pandas/python 如何根据python中另一个df的值过滤一个df - How to filter a df based on the values of another df in python 如何在Python中使用通用键将值映射到值 - How do I map values to values with a common key in Python python导入共享共同的价值? - python import shares common values? 如何使用另一个数据框添加数据框并基于列更新公共值 - how to add a dataframe with another dataframe and updated common values based on a column Python Dataframe:根据另一个数据帧更新数据框中列的值 - Python Dataframe : Update the values of a column in a dataframe based on another dataframe 根据python另一列中的公共值在数据框的一列中划分2个值 - Dividing 2 values in a column of dataframe based on common value in another column in python 对于第一个 df 中值的所有实例,如何用另一个 dataframe 的值替换 pandas 行? - How do I replace pandas rows with values of another dataframe for all instances of the value in the first df?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM