如何使用 pandas 根据给定时间表创建每个主题的频率表?

发布于 2025-01-11 23:08:08 字数 2194 浏览 0 评论 0原文

这是一个时间表,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤 2025-01-18 23:08:08

使用 melt 展平数据框,然后使用 pivot_table 重塑数据框:

out = (
  df.melt(var_name='Freq', value_name='Data', ignore_index=False).assign(variable=1)
    .pivot_table('Freq', 'Name', 'Data', fill_value=0, aggfunc='count')
    .loc[df.index]  # sort by original index: Monday > Thuesday > ...
)

输出:

>>> out
Data       Data Mining  Data Science  Embedded Systems  Industrial Psychology  Project
Name                                                                                  
Monday               1             1                 1                      1        3
Tuesday              0             1                 1                      1        4
Wednesday            0             1                 0                      0        6
Thursday             2             0                 1                      1        3
Friday               1             1                 1                      1        3

Use melt to flatten your dataframe then pivot_table to reshape your dataframe:

out = (
  df.melt(var_name='Freq', value_name='Data', ignore_index=False).assign(variable=1)
    .pivot_table('Freq', 'Name', 'Data', fill_value=0, aggfunc='count')
    .loc[df.index]  # sort by original index: Monday > Thuesday > ...
)

Output:

>>> out
Data       Data Mining  Data Science  Embedded Systems  Industrial Psychology  Project
Name                                                                                  
Monday               1             1                 1                      1        3
Tuesday              0             1                 1                      1        4
Wednesday            0             1                 0                      0        6
Thursday             2             0                 1                      1        3
Friday               1             1                 1                      1        3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文