sam
sam

Reputation: 19164

Plotting graph using pandas dataframe for multiple columns

I have a csv file with name "file.csv"

,DATE,DAY,OPEN,2PM,CLOSE,STATUS
0,2021-05-18,Tuesday,538.8,530.45,530.8,0
1,2021-05-19,Wednesday,530.65,532.6,536.85,0
2,2021-05-20,Thursday,536.95,537.05,536.35,1
3,2021-05-21,Friday,538.0,538.2,537.55,1
4,2021-05-24,Monday,537.3,535.05,532.85,1
5,2021-05-25,Tuesday,535.9,531.35,529.65,1
6,2021-05-26,Wednesday,532.95,530.55,532.1,0
7,2021-05-27,Thursday,532.95,529.65,529.85,0

I am using pandas to convert it to df.

import pandas as pd
df = pd.read_csv("file.csv")

df output can be seen as

enter image description here

There is a "STATUS" column that has 1 and 0 values.

I want to plot Monday to Friday from DAY column on the graph with values of STATUS column (i.e. 0 or 1). I want to see how many percentages of 0 or 1 were present on each days.

I do not know how to use these 2 columns for plotting such a graph.

Any help is appreciated.

Upvotes: 2

Views: 240

Answers (2)

tdy
tdy

Reputation: 41327

To plot STATUS per DAY in pie form, one idea is a nested pie chart, e.g.:

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
g = df.groupby('DAY')['STATUS'].value_counts().unstack().fillna(0).reindex(days)

# STATUS       0    1
# DAY                
# Monday     0.0  1.0
# Tuesday    1.0  1.0
# Wednesday  2.0  0.0
# Thursday   0.0  1.0
# Friday     0.0  1.0
# outer ring (DAY)
size = 0.2
plt.pie(g.sum(axis=1), radius=1,
        startangle=90, counterclock=False,
        wedgeprops=dict(width=size, edgecolor='w'),
        labels=g.index)

# inner ring (STATUS)
values = g.values.ravel()
status = g.columns.tolist() * len(g)
colors = ['#999', '#333'] * len(g)
plt.pie(values, radius=1 - size, colors=colors,
        startangle=90, counterclock=False,
        wedgeprops=dict(width=size, edgecolor='w'),
        labels=[status[i] if values[i] else '' for i in range(len(values))],
        labeldistance=0.6)

nested pies for status per day

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153460

IIUC, do you want something like this:

df.groupby('DAY')['STATUS'].value_counts(normalize=True).unstack().plot.bar()

Output:

enter image description here

Pie chart:

df.groupby('DAY')['STATUS'].value_counts(normalize=True).unstack(0).plot.pie(subplots=True, figsize=(15,8))

Output:

enter image description here

Upvotes: 2

Related Questions