-1

Dataset

Age    factor1    factor2
20-25    0           1
26-30    0           0
31-35    1           0
20-25    1           1
26-30    0           1
20-25    1           0

I want to a line graph in python and groupby the value 'Age' (x-axis) and y-axis is the count of the number of times '1' comes in the factor1 column.

I tried the code below but it shows the count of number of times the 'age' category repeats instead of the count of 1 in factor1

df.groupby('Age')['factor1'].count().plot(kind='line')
plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Ajinkya
  • 201
  • 4
  • 11

1 Answers1

1

You can using crosstab

pd.crosstab(df.Age,df.factor1)
factor1  0  1
Age          
20-25    1  2
26-30    2  0
31-35    0  1

pd.crosstab(df.Age,df.factor1)[1].plot(kind='line')

enter image description here

BENY
  • 317,841
  • 20
  • 164
  • 234
  • 1
    How can I get names on the x-axis from the Age column? When I change the kind of graph to bar it shows but not in line – Ajinkya Dec 02 '18 at 15:58
  • @Ajinkya check https://stackoverflow.com/questions/21487329/add-x-and-y-labels-to-a-pandas-plot, – BENY Dec 02 '18 at 18:36
  • I checked that, it only sets one label for the x-axis as it shows in the above image i.e Age. But I want to print the values in Age there like 20-25, 26-30 and 31-35 – Ajinkya Dec 02 '18 at 20:10
  • @Ajinkya how about this https://stackoverflow.com/questions/49961169/show-categorical-x-axis-values-when-making-line-plot-from-pandas-series-in-matpl – BENY Dec 02 '18 at 22:44
  • Thanks, that fixed my problem. – Ajinkya Dec 02 '18 at 23:07