-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup Bar chart (count by native-country ).py
60 lines (42 loc) · 1.59 KB
/
Group Bar chart (count by native-country ).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#read used columns
df = pd.read_csv (r'project_data.csv', usecols=[6,9,11,13,14,15])
#draw pie chart for occupation
query = df[['native-country', 'class']].groupby(['native-country', 'class']).size().reset_index(name='counts')
query.dropna()
query2 = df[['native-country']].groupby(['native-country']).size().reset_index(name='counts')
#print(query)
# 1 => 1 : 26
start = 0
end = 27
labels = query['native-country'].unique()
class1 = query.loc[(query['class'] == 1)]['counts'].tolist()
class2 = query.loc[(query['class'] == 0)]['counts'].tolist()
#for index, row in query.iterrows():
# print(row)
query2['class2'] = np.ceil((class2 / query2['counts']) *100)
query2['class1'] = (100 - query2['class2'])
print (query2)
x = np.arange(len(query2['native-country'])) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, query2['class1'], width, label='>50K')
rects2 = ax.bar(x + width/2, query2['class2'], width, label='<=50K')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Percentage %')
ax.set_title('Class Percentage of native-country')
ax.set_xticks(x)
plt.xticks(rotation=90)
ax.set_xticklabels(labels)
ax.legend()
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
fig.tight_layout()
plt.show()
#print(df[(df['class'] == 0) & (df['occupation'] != 0)].count())
#plt.pie(df['occupation'].value_counts(), labels=df['occupation'].unique(), shadow=False, autopct='%1.2f%%')
#plt.axis('equal')
#plt.show()
#print(df['class'])