繁体   English   中英

在 2 个 csv 文件上使用 Pandas 进行左合并

[英]Left merge using pandas on 2 csv files

我有 2 张 csv 表:

我正在尝试找到一种将 table2 合并到 table1 的方法。 只要 table1 和 table2 具有相同的 Name 值,则将 table1 中的相应价格替换为 table2 中找到的价格,否则保持 table1 不变。

当前代码:

table1 = pd.read_csv('path/table1.csv', index_col=0)
table2 = pd.read_csv('path/table2.csv', index_col=0)
print(table1)
print(table2)

new_table = table1[["Name ", "ATT1", "ATT2"]].merge(table2[["Price", "Name "]], on="Name ", how="left")
print(new_table)

但是,这会导致以下情况:

   Price  Name   ATT1  ATT2
0     12   APPL    69    81
1    900  GOOGL   303   392
2     32    INV    39     9
   Price     Name 
0   1231      APPL
1     39  FACEBOOK
   Name   ATT1  ATT2   Price
0   APPL    69    81  1231.0
1  GOOGL   303   392     NaN
2    INV    39     9     NaN

我想要 new_table 打印的是:

   Name   ATT1  ATT2   Price
0   APPL    69    81  1231.0
1  GOOGL   303   392     900
2    INV    39     9     32

在合并之前从 table1 中drop “Price”列:

new_table = table1.drop("Price", axis=1).merge(table2, on="Name", how="left")

>>> new_table
    Name  ATT1  ATT2   Price
0   APPL    69    81  1231.0
1  GOOGL   303   392     NaN
2    INV    39     9     NaN

顺便说一句,两个表中的“未命名:0”列可能是由于 csv 文件中的索引列未命名。 您可以通过将index_col=0传递给pd.read_csv来避免这种情况, pd.read_csv所示:

table1 = pd.read_csv('path/table1.csv', index_col=0)
table2 = pd.read_csv('path/table2.csv', index_col=0)

或者,只使用您在merge需要的列:

new_table = table1[["Name", "ATT1", "ATT2"]].merge(table2[["Price", "Name"]], on="Name", how="left")

暂无
暂无

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

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