我如何在Python中实现这一点。我知道excel中有一个vlookup函数,但是如果在Python中有一种方法,我更喜欢用Python来实现它。基本上,我的目标是从CSV2列数量中获取数据,并将数据写入基于Bin_Name的CSV1的列数量。脚本不应该一次复制所有的值,它必须通过选择一个Bin_Name。例:今天,我想把Bin_Name的数据从CSV2的ABCDE到CSV1,然后它会用CSV1的列数量来写数据。如果这是可能的话,我将非常感激,并将从中学到很多东西。先谢谢你。
CSV1 CSV2
Bin_Name Quantity Bin_Name Quantity
A A 43
B B 32
C C 28
D D 33
E E 37
F F 38
G G 39
H H 41
发布于 2022-12-02 00:57:56
这里有一种方法可以在Python中实现这一点而无需使用
以下是上述步骤的示例实现:
# Import the csv module to read and write CSV files
import csv
# Open the two CSV files in read mode
with open("CSV1.csv", "r") as csv1_file, open("CSV2.csv", "r") as csv2_file:
# Use the csv reader to read the CSV files into lists of dictionaries
csv1_reader = csv.DictReader(csv1_file)
csv1_data = list(csv1_reader)
csv2_reader = csv.DictReader(csv2_file)
csv2_data = list(csv2_reader)
# Iterate over the list of dictionaries from CSV1
for row in csv1_data:
# Search for a matching Bin_Name in the list of dictionaries from CSV2
match = next((r for r in csv2_data if r["Bin_Name"] == row["Bin_Name"]), None)
# If a match is found, update the Quantity value in the dictionary from CSV1
# with the Quantity value from the matching dictionary in CSV2
if match:
row["Quantity"] = match["Quantity"]
# Open a new CSV file in write mode
with open("updated_csv1.csv", "w") as updated_csv1_file:
# Use the csv writer to write the updated list of dictionaries to the new CSV file
csv1_writer = csv.DictWriter(updated_csv1_file, fieldnames=csv1_reader.fieldnames)
csv1_writer.writeheader()
csv1_writer.writerows(csv1_data)
发布于 2022-12-02 00:53:45
嗨,您可以先迭代CSV2,然后收集想要的值之后,可以在CSV1中搜索它。我在下面写了一段代码,它可能对你有帮助,但是可以有更有效的方法来做。
def func(wanted_rows: list,csv2df: pd.DataFrame):
# Iterate csv2df
for index,row in csv2df.iterrows():
# Check if index in the wanted list
if index in wanted_rows:
# Get index of CSV1 for same value
csv1_index = CSV1[CSV1.Bin_Name == row['Bin_Name']].index[0]
CSV1.at[csv1_index,'Quantity'] = row['Quantity']
return df
wanted_list = [1,2,3,4,5]
func(wanted_list,CSV2df)
发布于 2022-12-02 01:12:21
在这种情况下,我只需要使用熊猫内置的函数,就不需要循环了。
因此,假设没有重复的bin名称,请尝试下面的代码来复制整个列:
df1= pd.read_csv("file1.csv")
df2= pd.read_csv("file2.csv")
df1["Quantity"]= df2["Quantity"].where(df1["Bin_Name"].eq(df2["Bin_Name"]))
print(df1)
Bin_Name Quantity
0 A 43
1 B 32
2 C 28
3 D 33
4 E 37
5 F 38
6 G 39
7 H 41
如果只需要复制行的子集,请将布尔索引与pandas.DataFrame.loc
一起使用:
vals= ["A", "B", "C", "D"]
df1.loc[df1["Bin_Name"].isin(vals), "Quantity"] = df2.loc[df1["Bin_Name"].isin(vals), "Quantity"]
print(df1)
Bin_Name Quantity
0 A 43.0
1 B 32.0
2 C 28.0
3 D 33.0
4 E NaN
5 F NaN
6 G NaN
7 H NaN
https://stackoverflow.com/questions/74652883
复制