简体   繁体   中英

Look up in DataFrame

I have a data frame as below:

index    name      col1   col2     count

"there is some values"


col1 : is the index of a value in df
col2 : is the index of a value in df

I want find the name related to index in col 1 and col 2 and put them in the match 1 and match 2. I want to sum the count of the current index and the matched index. For ex, for the first row, I want to sum the count of first row and matched row.

I have been trying from the yesterday but I couldn't manage to produce the result. can any one help me?

what I want is:


index     name       col1     col 2      count     match 1   match 2    sum
1          x1          5       3           2        x5         x3      2+7+2
2          x2          4       6           3        x4         x6       3+1+1
3          x3                              7       
4          x4                              1
5          x5                              2
6          x6                              1

If I understood the question correctly, I believe this will get you there:

df = pd.DataFrame([
        ['x1',      5,      3, 2, 'x5', 'x3'],
        ['x2',      4,      6, 3, 'x4', 'x6'],
        ['x3', np.nan, np.nan, 7,           ],
        ['x4', np.nan, np.nan, 1,           ],
        ['x5', np.nan, np.nan, 2,           ],
        ['x6', np.nan, np.nan, 1,           ],
    ], columns=['name', 'col1', 'col2', 'count', 'match1', 'match2'])
df2 = df.merge(df, how='inner', left_on='match1', right_on='name', suffixes=('', '_match1'))
df3 = df2.merge(df, how='inner', left_on='match2', right_on='name', suffixes=('', '_match2'))
df3['sum'] = df3['count'] + df3['count_match1'] + df3['count_match2']
df3 = df3[list(df.columns) + ['sum']]

>>> print(df3)
  name  col1  col2  count match1 match2  sum
0   x1   5.0   3.0      2     x5     x3   11
1   x2   4.0   6.0      3     x4     x6    5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM