2016-03-27 13 views
1

Werte würde ich als so einen Datenrahmen konstruieren mag:Konstrukt Datenrahmen Zeilen mit Spalten zählt als

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 

Allerdings gibt diese Spaltenüberschriften nur. Ich möchte vermeiden, den Spaltenzählungen Variablennamen zuzuordnen ...

P.S. Der Grund dafür ist, die Werte in ein gruppiertes Balkendiagramm unter Verwendung von Matplotlib & plotly

Antwort

0

Ich weiß nicht, was die gewünschte Ausgabe ist.
Also versuche ich, mehr Möglichkeiten:

können Sie verwenden DataFrame, set_index und T:

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 

Oder Sie können from_dict verwenden:

#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 

Hinweis: count nicht NaN Werte zählt in Spalten.

+0

Count gibt mir eine numpy int32 z. (576) für meine Spaltenanzahl (zB meeting_participants_df ['Attended Meetings']. Count()). Ich möchte bei der Erstellung meines Datenrahmens auf die Spaltenanzahl für die Werte in meinem Datenrahmen verweisen. –

+0

Verweise sind problematisch, wenn 'df' durch' pd.DataFrame' erstellt wird. Aber was ist dein gewünschter Output? – jezrael

+0

Ich werde ein gruppiertes Histogramm daraus machen, mit Matplotlib (oder Pandas Plotting Lib?) Und plotly –

Verwandte Themen