1

How can i convert a dataframe into series of Pandas series. My dataframe below and i want it to plot with stacked bar chart.

    city  soru_id value1 value2 value3 value4 value5
0      1        2    147    119     69     92    106
1      2        2     31     20     12     14     26
2      3        2     37     22     24     18     19
3      4        2     10     13      7     13     10
4      5        2     38     48     18     30     27
5      6        2    401    409    168    354    338
..   ...      ...    ...    ...    ...    ...    ...
76    77        2     12      7      3     12      8
77    78        2      4      2      1     12      3
78    79        2      3   None      1   None   None
79    80        2     12      7      4      4      7
80    81        2     18     13      7     10      2

[81 rows x 7 columns]

Therefore i need to get the dataframe into form like below to plot it. I can not loop into DataFrame() method. How can i plot it.

df = pd.DataFrame([pd.Series([value1,value2,value3,value4,value5]), 
        pd.Series([value1,value2,value3,value4,value5]), 
        pd.Series([value1,value2,value3,value4,value5]),
        ..................................
        ..................................
        pd.Series([value1,value2,value3,value4,value5]),
        pd.Series([value1,value2,value3,value4,value5])
        ], index=[index0,index2,index3,....,index79,index80])

df.plot.bar(stacked=True)
plt.show()
3
  • This seems like an XY problem. You just want to plot the values from each row? Commented Jul 17, 2018 at 16:08
  • Each row in the dataframe is already a series. Commented Jul 17, 2018 at 16:11
  • What does your desired output chart look like? Commented Jul 17, 2018 at 17:57

1 Answer 1

1

You don't need to change anything about your dataframe, just index the dataframe based on your value columns and plot that.

cols = [col for col in df.columns if col.startswith('value')]
df[cols].plot.bar(stacked=True)
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.