简体   繁体   English

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

[英]Left merge using pandas on 2 csv files

I have 2 csv sheets:我有 2 张 csv 表:

I am trying to find a way to merge table2 onto table1.我正在尝试找到一种将 table2 合并到 table1 的方法。 Whenever table1 and table2 have the same Name value then replace the corresponding Price in table1 with the one found in table2 otherwise leave table1 as is.只要 table1 和 table2 具有相同的 Name 值,则将 table1 中的相应价格替换为 table2 中找到的价格,否则保持 table1 不变。

Current code:当前代码:

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)

However, this leads to the following:但是,这会导致以下情况:

   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

What i want new_table to print is:我想要 new_table 打印的是:

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

drop the "Price" column from the table1 before merging:在合并之前从 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

As an aside, the "Unnamed: 0" columns in both your tables are likely due to the index column being unnamed in your csv files.顺便说一句,两个表中的“未命名:0”列可能是由于 csv 文件中的索引列未命名。 You can avoid this by passing index_col=0 to pd.read_csv like so:您可以通过将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)

Alternatively, only use the columns you need in the merge :或者,只使用您在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