首页 > 解决方案 > 熊猫:来自不同行的相等值

问题描述

数据框示例:

    A           B
0   2.234560    0.842407
1   0.842407    2.234560
2   3.183770    1.200300
3   1.200300    3.18377

我正在尝试添加一个关于“A”和“B”列中的值匹配的新列

像这样:

    A           B           C
0   2.234560    0.842407    A0 Matching with B1
1   0.842407    2.234560    B1 Matching with A0 
2   3.183770    1.200300    A2 Matching with B3
3   1.200300    3.183770    B3 Matching with A2 

什么是正确的解决方案?

标签: pythonpandas

解决方案


一些步骤可以改进,但应该有效:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(4,1), columns=['A'])
df['B'] = df['A'].sample(frac=1).reset_index(drop=True)


df_temp_a = df[['A']].assign(col_string_a = lambda x: 'A'+x.index.astype('str')).set_index('A')
df_temp_b = df[['B']].assign(col_string_b = lambda x: 'B'+x.index.astype('str')).set_index('B')

merged = df_temp_a.join(df_temp_b, how='inner')
merged['final_col'] = merged['col_string_a'] + " Matching with " + merged['col_string_b']
merged

df.merge(merged[['final_col']], how='left', left_on=['A'], right_index=True)

推荐阅读