构造列计数为值的数据帧行

2024-05-23 14:16:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想构建一个如下的数据帧:

raw_data = {'Users Status': ['Attended', 'Facilitated', 'Hosted'],
    'previous_week': [meeting_participants_df['Attended Meetings'].count(), meeting_facilitators_df['Facilitated Meetings'].count(), meeting_owners_df['Hosted Meetings'].count()],
    'current week': [meeting_participants_df2['Attended Meetings'].count(), meeting_facilitators_df2['Facilitated Meetings'].count(), meeting_owners_df2['Hosted Meetings'].count()]}
host_facilitators_participants = pd.DataFrame(raw_data, columns = ['Attended', 'Facilitated', 'Hosted'])
host_facilitators_participants

但是,这只返回列标题。我想避免给列计数指定变量名。。。你知道吗

另外,这样做的原因是使用matplotlib&plotly将值放入分组条形图中


Tags: hostdfdatarawcountdf2weekhosted
1条回答
网友
1楼 · 发布于 2024-05-23 14:16:00

我不知道期望的输出是什么。
所以我尝试了更多的可能性:

可以使用^{}^{}^{}

print raw_data   
{ 'current week': [2, 4, 3], 
  'Users Status': ['Attended', 'Facilitated', 'Hosted'], 
 'previous_week': [2, 4, 3]}

#omit column names 
host_facilitators_participants = pd.DataFrame(raw_data)
print host_facilitators_participants
  Users Status  current week  previous_week
0     Attended             2              2
1  Facilitated             4              4
2       Hosted             3              3

#set index from column Users Status
host_facilitators_participants = host_facilitators_participants.set_index('Users Status')
print host_facilitators_participants
              current week  previous_week
Users Status                             
Attended                 2              2
Facilitated              4              4
Hosted                   3              3

#transpose dataframe
host_facilitators_participants = host_facilitators_participants.T
print host_facilitators_participants
Users Status   Attended  Facilitated  Hosted
current week          2            4       3
previous_week         2            4       3

或者可以使用^{}

#omit 'Users Status': ['Attended', 'Facilitated', 'Hosted'] from dictionary    
print raw_data1  
{'current week': [2, 4, 3], 'previous_week': [2, 4, 3]}

#use from_dict for creating dataframe,  keys of dict should be rows
host_facilitators_participants = pd.DataFrame.from_dict(raw_data1, orient='index')
#set column names
host_facilitators_participants.columns=['Attended', 'Facilitated', 'Hosted']
print host_facilitators_participants
               Attended  Facilitated  Hosted
current week          2            4       3
previous_week         2            4       3

#set index in dataframe constructor
host_facilitators_participants = pd.DataFrame(raw_data1, 
                                              index=['Attended', 'Facilitated', 'Hosted'])
print host_facilitators_participants
             current week  previous_week
Attended                2              2
Facilitated             4              4
Hosted                  3              3

注意:^{}不计算列中的NaN值。你知道吗

相关问题 更多 >