最初我有两个数据帧:
数据帧A:
Column1 Column2
0 X-5 H4
1 X-3 H4
2 X-4 H5
3 X-5 H5
4 X-5 H-3
5 X-3 H-3
6 X-3 H-3
7 X-4 H-4
8 X-4 H-5
9 X-3 H-4
10 X-5 H-3
11 X-3 H-3
12 X-5 H-5
13 X-5 H-3数据帧2:
Column1 Hours
0 X-5 12
1 X-4 14
2 X-3 10我希望能够在满足"Column1“条件时将两个数据帧连接成一个单独的数据帧,此外还能够消除重复行并计算它们重复的次数:
Column1 Column2 Repeat Hours
0 H3 X-3 3 14
1 H-3 X-5 3 12
2 H-4 X-3 2 14
3 H-4 X-4 1 10
4 H-4 X-5 1 12
5 H-5 X-4 2 10
6 H-5 X-5 2 12如果你能帮助我,我将不胜感激,因为我不太知道怎么做。提前感谢
发布于 2021-11-23 22:29:35
这是你想要的吗?
a.Column2 = a.Column2.str.replace("H(\d)", "H-\\1", regex=True)
pd.merge(a, b, on="Column1").groupby(["Column2", "Column1"], as_index=False).agg(
Repeat=("Hours", "size"), Hours=("Hours", "first")
)
# Column2 Column1 Repeat Hours
# 0 H-3 X-3 3 10
# 1 H-3 X-5 3 12
# 2 H-4 X-3 2 10
# 3 H-4 X-4 1 14
# 4 H-4 X-5 1 12
# 5 H-5 X-4 2 14
# 6 H-5 X-5 2 12https://stackoverflow.com/questions/70088536
复制相似问题