1
    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.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
user3447653
  • 3,968
  • 12
  • 58
  • 100

2 Answers2

1

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

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Can we place pie chart and a table side by side in a notebook? For example, I have a pie chart using "%%chart pie --fields value,count --data df2" and a table using "%%chart table --fields MachineName --data df_result2" But instead of placing pie chart and table separately, I need to have it side by side. Is there an option for this in IPython? – user3447653 Jul 12 '16 at 19:37
  • Hard question for me, because I never use `IPython` this way, but try [this](http://stackoverflow.com/questions/8524401/how-can-i-place-a-table-on-a-plot-in-matplotlib). I know in `pandas` if use only `plot` - [tables wih graph](http://pandas.pydata.org/pandas-docs/stable/visualization.html#plotting-tables) – jezrael Jul 12 '16 at 19:48
0

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')

Victor Chubukov
  • 1,345
  • 1
  • 10
  • 18