如何使用 pandas 根据给定时间表创建每个主题的频率表?
这是一个时间表,columns=hour,rows=weekday,data=subject [weekday x hour]
1 2 3 4 5 6 7
Name
Monday Project Project Project Data Science Embedded Systems Data Mining Industrial Psychology
Tuesday Project Project Project Project Data Science Industrial Psychology Embedded Systems
Wednesday Data Science Project Project Project Project Project Project
Thursday Data Mining Industrial Psychology Embedded Systems Data Mining Project Project Project
Friday Industrial Psychology Embedded Systems Data Science Data Mining Project Project Project
如何生成 pandas.Dataframe
其中,rows=weekday,columns=subject,data = subject相应工作日的频率?
所需表格:[工作日 x 主题]
Data Mining, Data Science, Embedded Systems, Industrial Psychology, Project
Name
Monday 1 1 1 1 3
Tuesday ...
Wednesday
Thursday
Friday
self.file = 'timetable.csv'
self.sdf = pd.read_csv(self.file, header=0, index_col="Name")
print(self.sdf.to_string())
self.subject_frequency = self.sdf.apply(pd.value_counts)
print(self.subject_frequency.to_string())
self.subject_frequency["sum"] = self.subject_frequency.sum(axis=1)
This is a time table, columns=hour, rows=weekday, data=subject [weekday x hour]
1 2 3 4 5 6 7
Name
Monday Project Project Project Data Science Embedded Systems Data Mining Industrial Psychology
Tuesday Project Project Project Project Data Science Industrial Psychology Embedded Systems
Wednesday Data Science Project Project Project Project Project Project
Thursday Data Mining Industrial Psychology Embedded Systems Data Mining Project Project Project
Friday Industrial Psychology Embedded Systems Data Science Data Mining Project Project Project
How do you generate a pandas.Dataframe
where, rows=weekday, columns=subject, data = subject frequency in the corresponding weekday?
Required table: [weekday x subject]
Data Mining, Data Science, Embedded Systems, Industrial Psychology, Project
Name
Monday 1 1 1 1 3
Tuesday ...
Wednesday
Thursday
Friday
self.file = 'timetable.csv'
self.sdf = pd.read_csv(self.file, header=0, index_col="Name")
print(self.sdf.to_string())
self.subject_frequency = self.sdf.apply(pd.value_counts)
print(self.subject_frequency.to_string())
self.subject_frequency["sum"] = self.subject_frequency.sum(axis=1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
melt
展平数据框,然后使用pivot_table
重塑数据框:输出:
Use
melt
to flatten your dataframe thenpivot_table
to reshape your dataframe:Output: