简体   繁体   English

Pandas 比较两个数据框中的两列,如果有匹配项,则从第三列获取值转换为周数

[英]Pandas compare two columns from two dataframes if there is a match get value from third column convert to week number

truing to automate one report, but its over my current skill level, please assist if possible.试图使一份报告自动化,但它超出了我目前的技能水平,请尽可能提供帮助。 df1 df1

report = {'Name':["one", "two", "three", "four", "five"],'Week_Num':['','','','',''],"Week_1":['1','1','','',''],"Week_2":['','1','','1',''],"Week_3":['','','','','1'],"Week_4":['','','1','',''],"Week_5":['','','','','']}

df2 DF2

data = {'Name': ["one", "two", "four", "five", "two", "three"],
    "Dates":["03/01/2022", "09/01/2022", "13/01/2022", "21/01/2022", "01/01/2022","28/01/2022"]}

Now i have to match report['Name'] with data['Name'],if there is a match to get data["Dates"] convert data["Dates"] to week_number"Today is week 18", and increase value of report.at[match, week_number] += 1 on every match.现在我必须将 report['Name'] 与数据 ['Name'] 匹配,如果匹配得到 data["Dates"] 将 data["Dates"] 转换为 week_number"Today is week 18", and increase每场比赛的 report.at[match, week_number] += 1 的值。 Expected output:预计 output:

report = {'Name':["one", "two", "three", "four", "five"],'Week_Num':['','','','',''],"Week_1":['1','1','','',''],"Week_2":['','1','','1',''],"Week_3":['','','','','1'],"Week_4":['','','1','',''],"Week_5":['','','','','']}

Try with merge :尝试merge

data["Week_Num"] = pd.to_datetime(data["Dates"],format="%d/%m/%Y").dt.strftime("%W").astype(int).add(1)

counts = data[["Name","Week_Num"]].merge(report["Name"], how="right").set_index("Name")
output = pd.get_dummies(counts["Week_Num"]).reindex(range(1,6),axis=1,fill_value=0).groupby(level=0).sum().reindex(report["Name"]).reset_index()

>>> output
    Name  1  2  3  4  5
0    one  0  1  0  0  0
1    two  1  1  0  0  0
2  three  0  0  0  0  1
3   four  0  0  1  0  0
4   five  0  0  0  1  0

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

相关问题 Pandas 比较不同数据帧的两列,如果匹配,则复制第三列的值 - Pandas compare two columns of different dataframes and copy value of a third column if there is a match Pandas 数据框 - 匹配两个数据框中的两列以更改第三列的值 - Pandas dataframes - Match two columns in the two dataframes to change the value of a third column 比较来自两个不同数据框熊猫的列 - Compare columns from two different dataframes pandas 如何比较两列并从第三列返回值 Pandas dataframe - How to compare two columns and return value from a third column in Pandas dataframe 比较来自不同数据框的两个系列,并将找到的匹配项替换为第三个系列值 - Compare two series from different dataframes, and where match found replace with third series value 需要比较 pandas 中两个数据帧的两列 - Need to compare two columns from two dataframes in pandas Dataframe - 对于每一行,比较两列的值,匹配时获取第三列的值 - Dataframe - for each row, compare values of two columns, get value of third column on match pandas:比较来自两个不同大小的不同数据帧的字符串列 - pandas: compare string columns from two different dataframes of different sizes 如何比较两个数据框并从列中查找匹配项(熊猫) - How to compare two dataframes and find matches from columns (pandas) 比较来自两个数据帧的值 - Compare value from two dataframes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM