构造具有列计数值的数据帧行

时间:2016-03-27 02:31:34

标签: python python-2.7 pandas dataframe

我想构建一个数据帧:

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

但是,这仅返回列标题。我想避免将变量名称分配给列计数...

P.S。这样做的原因是使用matplotlib& amp;将值放入分组的条形图中。 plotly

1 个答案:

答案 0 :(得分:0)

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

您可以使用DataFrameset_indexT

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

或者您可以使用from_dict

#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

注意:count不计算列中的NaN值。