给出以下样本数据集:
import numpy as np
import pandas as pd
df1 = (pd.DataFrame(np.random.randint(3, size=(5, 4)), columns=('ID', 'X1', 'X2', 'X3')))
print(df1)
ID X1 X2 X3
0 2 2 0 2
1 1 0 2 1
2 1 2 1 1
3 1 2 0 2
4 2 0 0 0
d = {'ID' : pd.Series([1, 2, 1, 4, 5]), 'Tag' : pd.Series(['One', 'Two', 'Two', 'Four', 'Five'])}
df2 = (pd.DataFrame(d))
print(df2)
ID Tag
0 1 One
1 2 Two
2 1 Two
3 4 Four
4 5 Five
df1['Merged_Tags'] = df1.ID.map(df2.groupby('ID').Tag.apply(list))
print(df1)
ID X1 X2 X3 Merged_Tags
0 2 2 0 2 [Two]
1 1 0 2 1 [One, Two]
2 1 2 1 1 [One, Two]
3 1 2 0 2 [One, Two]
4 2 0 0 0 [Two]
ID = 1
的预期输出:
1
每个密钥如何分组并在Tag: Frequency
列中生成Merged_Tags
格式?
ID X1 X2 X3 Merged_Tags
1 1 0 2 1 [One: 3, Two: 3]
2
为具有ID
ID X1 X2 X3 Merged_Tags Frequency
1 1 0 2 1 [One: 3, Two: 3] 3
第3
在每个行事件中添加列X3
的值,并使用相同的ID
ID X1 X2 X3 Merged_Tags Frequency X3++
1 1 0 2 1 [One: 3, Two: 3] 3 4
答案 0 :(得分:0)
1 0 2 1 [One: 3, Two: 3]
应该是[One:2,Two:3]而不是吗?考虑到:
1 : [One,Two]
0 : None
2 : [Two]
1 : [One, Two]
你想要一行中每个键的总计数器吗?
请帮助我理解[One:3,Two:3]背后的直觉,以防我在这里遗漏任何东西,但你的问题应该很容易解决,否则