Home > Mobile >  plot dataframe with pandas (python)
plot dataframe with pandas (python)

Time:12-05

Im trying to plot a csv file which looks like that:

enter image description here

I already changed the types of the columns to be fine for a plot

df.dtypes

Unnamed: 0      int64
Kanton         object
Stadt          object
1850          float64
1860          float64
1870          float64
1880          float64
1890          float64
1900          float64
1910          float64
1920          float64
1930          float64
1940          float64
1950          float64
1960          float64
1970          float64
1980          float64
1990          float64
2000          float64
dtype: object 

I put all my "Stadt"-Names into an array:

df.Stadt.unique()

this command seems not to be working, i get the error that

ax = df.Stadt.plot(kind='bar', color='#c93867',figsize=(50,10),columns="1950")
...
TypeError: no numeric data to plot

My column "1950" is of type float tho as shown above

Thank you in advance

CodePudding user response:

The problem is you subset the dataframe using Stadt and there is no numeric data to plot. Try:

ax = df.plot(kind='bar', x='Stadt', y=1950, color='#c93867', figsize=(50, 10))

Demo:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Kanton': ['AG', 'AG', 'AG'],
                   'Stadt': ['A', 'B', 'C'],
                   1950: [1, 2, 3], 1960: [10, 11, 12]})

df.plot(kind='bar', x='Stadt', y=1950, color='#c93867', figsize=(50,10))
plt.show()

enter image description here

  • Related