代替换表如下:
表头 | 业务员ID | 县区机构 |
---|---|---|
1 | 342523197901080022 | 绩溪 |
2 | 342523199009180048 | 宁国 |
3 | 342523199009180048 | 宁国 |
4 | 342523198409030028 | 旌德 |
5 | 342523198409030028 | 旌德 |
6 | 342523198409030028 | 旌德 |
7 | 34252319571226101X | 广德 |
8 | 34252319571226101X | 广德 |
9 | 342523196206101624 | 泾县 |
10 | 342523196206101624 | 泾县 |
.. | .. | .. |
对比表:
表头 |
---|
342523199009180048 |
34252319571226101X |
342501199008246211 |
342501199106205835 |
342501199210128622 |
34250119950814641X |
342501200008170814 |
.. |
拟实现如下:
读取“对比表”的数据,然后在“待替换”表格的“业务员ID”列进行比较,如果在“业务员ID”列发现了“对比表”中的数据,则将“待替换表格”中横向对应的“县区机构列”的值替换为“宣州区”。(备注:实际表格有非常多的列数,这里是举例提取了关键性两列,想要实现直接搜索替换的功能,同时必须在“对比表”上进行替换和保存)
根据你的需求建立了两个表格,其中待替换表为data1,其内容如下:
用于存放ID的表为data2,其内容如下:
import pandas as pd
path1 = r'C:\Users\Desktop\data1.xlsx' ##待替换的表
path2 = r'C:\Users\Desktop\data2.xlsx' ##ID表
df1 = pd.read_excel(path1)
df2 = pd.read_excel(path2)
df1['ID'] = df1['ID'].astype('str')
mask = df1['ID'].isin(df2['表头'].tolist())
lic = df1[mask].index.values
df1.loc[lic, '地区'] = '宣州区'
print(df1)
运行的结果为:
如果问题得到解决的话请点 采纳~~~
以下是实现这个需求的示例代码:
import pandas as pd
# 读取表格数据
replace_df = pd.read_csv('待替换表格.csv')
compare_df = pd.read_csv('对比表.csv')
# 找出需要替换的行
replace_mask = replace_df['业务员ID'].isin(compare_df['表头'])
replace_rows = replace_df[replace_mask]
# 修改县区机构列的值
replace_rows['县区机构'] = '宣州区'
# 保存修改后的表格
replace_df.to_csv('待替换表格.csv', index=False)
python中字符串的查找替换操作
学习下
https://blog.csdn.net/qq_51432387/article/details/122736948
import pandas as pd
# Load the "待替换" table into a pandas DataFrame
df = pd.read_csv("待替换表格.csv")
# Load the "对比表" into a pandas Series
s = pd.read_csv("对比表.csv", header=None, squeeze=True)
# Iterate through the rows of the DataFrame
for i, row in df.iterrows():
# Check if the value in the "业务员ID" column is in the Series
if row["业务员ID"] in s:
# If it is, update the value in the "县区机构" column
df.at[i, "县区机构"] = "宣州区"
# Save the updated DataFrame to a new CSV file
df.to_csv("替换后表格.csv", index=False)
读取“对比表”和“待替换表格”
import pandas as pd
# 读取对比表
df_compare = pd.read_excel("对比表.xlsx")
# 读取待替换表格
df_replace = pd.read_excel("待替换表格.xlsx")
使用 merge 函数将两个表格按照“业务员ID”列合并
# 使用 merge 函数将两个表格按照“业务员ID”列合并
df_merged = pd.merge(df_compare, df_replace, on="业务员ID")
最后遍历 df_merged 中的每一行,如果“业务员ID”列的值在“对比表”中存在,则将“县区机构列”的值替换为“宣州区”
# 遍历 df_merged 中的每一行
for index, row in df_merged.iterrows():
# 如果“业务员ID”列的值在“对比表”中存在
if row["业务员ID"] in df_compare["业务员ID"].values:
# 将“县区机构列”的值替换为“宣州区”
df_replace.loc[df_replace["业务员ID"] == row["业务员ID"], "县区机构"] = "宣州区"
最后将替换后的“待替换表格”保存到文件中
df_replace.to_excel("待替换表格_替换后.xlsx", index=False)