我有一个DataFrame列author
(作者姓名),hour
(作者发布主题时)和number_of_topics
(每个作者一小时发布多少主题)。以下是一个例子:
author hour number_of_topics
0 A h01 1
1 B h02 4
2 B h04 2
3 C h04 6
4 A h05 8
5 C h05 3
我的目标是创建六列(前六个小时),并用主题数量填充它们。我尝试用df.groupby
来做这件事,但没有成功。
期望输出:
author h01 h02 h03 h04 h05 h06
0 A 1 0 0 0 8 0
1 B 0 4 0 2 0 0
2 C 0 0 0 6 3 0
创建我的数据帧的代码:
import pandas as pd
df = pd.DataFrame({"author":["A","B", "B","C","A","C"],
"hour":["h01","h02","h04","h04","h05","h05"],
"number_of_topics":["1","4","2","6","8","3"]})
print(df)
可以通过
pivot
函数来实现您所寻找的:将^{} 与^{} 一起用于添加错误的列:
或^{} 与^{} :
相关问题 更多 >
编程相关推荐