user3447653
user3447653

Reputation: 4158

Format a pandas dataframe

    MachineName EventLogs
0   P79         Yes
1   P14         Yes
2   P1          No
3   P93         No

I count the number of logs in a dataframe using the below statement:

df1 = pd.value_counts(df['Logs'].values, sort=False)

and when I print df1, the output is like below:

Yes  2
No   2
dtype: int64

I cannot create a pie chart using the result as I need to specify the column names while creating pie chart.

Something like

%%chart pie --fields Value,Count --data df1

Not sure on how to add the column names to df1. Any help would be appreciated.

Upvotes: 1

Views: 327

Answers (2)

jezrael
jezrael

Reputation: 863511

You need reset_index, because you have Series:

df1 = pd.value_counts(df['Logs'].values, sort=False).reset_index()
df1.columns = ['value','count']

With sample:

df1 = pd.value_counts(df['EventLogs'].values, sort=False).reset_index()
df1.columns = ['value','count']
print (df1)
  value  count
0    No      2
1   Yes      2

Or you can use value_counts:

df1 = df.EventLogs.value_counts().reset_index(name='Count').rename(columns={'index':'Value'})
print (df1)
  Value  Count
0   Yes      2
1    No      2

With sort=False:

df1 = df.EventLogs.value_counts(sort=False)
                  .reset_index(name='Count')
                  .rename(columns={'index': 'Value'})
print (df1)
  Value  Count
0    No      2
1   Yes      2

Another solution is Series.plot.pie - see visualization in docs:

import matplotlib.pyplot as plt
df.EventLogs.value_counts(sort=False).plot.pie()
plt.show()

graph

Upvotes: 1

Victor Chubukov
Victor Chubukov

Reputation: 1375

I don't know what you're using to make your pie charts, but pandas has some perfectly reasonable plotting capabilities (through matplotlib).

df1.plot(kind='pie')

Upvotes: 0

Related Questions