基于一列合并两个数据框

时间:2020-04-14 21:51:34

标签: python pandas

如何合并两个数据框

df1:

Name     height    weight
Michael  180        55
Larry    183        53
Shaq     210        100

df2:

   Relinquished DTD DNP outindefinitely outseason
   Larry         1   0      0             0
   Shaq          2   0      0             1

我想合并df2中除列Relinquished之外的所有列,以及如果Name列的值与Relinquished列的值匹配的情况下要应用的值。被放弃的栏有球员的名字,但是只有伤病记录。我想将不在“放弃”栏中的其余球员填为0。

预期输出:

Name     height    weight  DTD DNP outindefinitely outseason
Michael  180        55      0   0      0              0 
Larry    183        53      1   0      0              0 
Shaq     210        100     2   0      0              1

3 个答案:

答案 0 :(得分:1)

import pandas as pd

df1 = pd.DataFrame({"Name": ["Michael", "Larry", "Shaq"], "height":[180, 183, 210], "weight":[55,52,100]})
df2 = pd.DataFrame({"Relinquished": ["Larry", "David"], "DTD":[1, 2], "DNP":[0, 0], "outindefinitely":[0, 0], "outseason":[0, 1]})
df2.head()

退出:

      Relinquished  DTD  DNP  outindefinitely  outseason
0        Larry    1    0                0          0
1        David    2    0                0          1

然后,我们必须重命名列Relinquished

df2.rename({"Relinquished":"Name"}, axis=1, inplace=True)
df2.head()

出局:

    Name  DTD  DNP  outindefinitely  outseason
0  Larry    1    0                0          0
1  David    2    0                0          1

最后,

merged = df1.merge(df2, how="outer", on="Name").fillna(0)
print(merged)

出局:

      Name  height  weight  DTD  DNP  outindefinitely  outseason
0  Michael   180.0    55.0  0.0  0.0              0.0        0.0
1    Larry   183.0    52.0  1.0  0.0              0.0        0.0
2     Shaq   210.0   100.0  0.0  0.0              0.0        0.0
3    David     0.0     0.0  2.0  0.0              0.0        1.0

答案 1 :(得分:0)

import pandas as pd

data = {'Name': ['Michael', 'Larry', 'Shaq'], 'height': ['180', '183', '210'], 'weight': ['55', '53', '100'] }
data2 = {'Relinquished': ['Larry', 'Shaq'], 'DTD': ['1', '2'], 'DNP': ['0', '0'], 'outindefinitely': ['0', '0'], 'outseason': ['0', '1'] }

df1 = pd.DataFrame.from_dict(data)
df2 = pd.DataFrame.from_dict(data2)

final_df = pd.merge(df1, df2, how='outer', left_on='Name', right_on='Relinquished' )

final_df = final_df.fillna('0')

final_df

答案 2 :(得分:0)

您可以尝试这样做。如果需要,您甚至可以在一个衬里中简化它。

\#

在我的环境中对其进行了测试,它似乎可以正常工作

df2.rename(columns={"Relinquished": "Name"}, inplace=True)

df2 = df2.append(df1).fillna(0).groupby("Name").aggregate("sum")