Graph with Python, Pandas

2

I have to graph from a csv file similar to this:

Cedula,Genero,Placa,Tipo,Multa,Fecha
0931921407,Femenino,GYS-9575,Automovil,Grave,2016-06-13
0910703362,Masculino,GSF-7654,Automovil,Leve,2016-02-18
0931921408,Femenino,GOI-3298,Automovil,Muy grave,2016-01-04
0987654321,Masculino,ETR-9873,Camioneta,Grave,2016-08-05
0931921407,Femenino,GYS-9575,Automovil,Grave,2016-06-13
0910703362,Masculino,GSF-7654,Automovil,Leve,2016-02-18
0931921408,Femenino,GOI-3298,Automovil,Muy grave,2016-01-04
0987654321,Masculino,ETR-9873,Camioneta,Grave,2016-08-05
0931921407,Femenino,GYS-9575,Automovil,Grave,2016-06-13
0910703362,Masculino,GSF-7654,Automovil,Leve,2016-02-18
0931921408,Femenino,GOI-3298,Automovil,Muy grave,2016-01-04
0987654321,Masculino,ETR-9873,Camioneta,Grave,2016-08-05
0931921407,Femenino,GYS-9575,Automovil,Grave,2016-06-13

My main problem is that of all the ways I try I have the error of

Traceback (most recent call last):
  File "C:/Users/User1/Downloads/x/xx/aaa.py", line 14, in <module>
    my_plot = grp.plot(kind='bar')
  File "C:\python\lib\site-packages\pandas\core\groupby.py", line 311, in __call__
    return self._groupby.apply(f)
  File "C:\python\lib\site-packages\pandas\core\groupby.py", line 651, in apply
    return self._python_apply_general(f)
  File "C:\python\lib\site-packages\pandas\core\groupby.py", line 655, in _python_apply_general
    self.axis)
  File "C:\python\lib\site-packages\pandas\core\groupby.py", line 1527, in apply
    res = f(group)
  File "C:\python\lib\site-packages\pandas\core\groupby.py", line 647, in f
    return func(g, *args, **kwargs)
  File "C:\python\lib\site-packages\pandas\core\groupby.py", line 309, in f
    return self.plot(*args, **kwargs)
  File "C:\python\lib\site-packages\pandas\tools\plotting.py", line 3740, in __call__
    sort_columns=sort_columns, **kwds)
  File "C:\python\lib\site-packages\pandas\tools\plotting.py", line 2614, in plot_frame
    **kwds)
  File "C:\python\lib\site-packages\pandas\tools\plotting.py", line 2441, in _plot
    plot_obj.generate()
  File "C:\python\lib\site-packages\pandas\tools\plotting.py", line 1026, in generate
    self._compute_plot_data()
  File "C:\python\lib\site-packages\pandas\tools\plotting.py", line 1135, in _compute_plot_data
    'plot'.format(numeric_data.__class__.__name__))
TypeError: Empty 'DataFrame': no numeric data to plot

this being what I write in the code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


lectura=pd.read_csv("dataset.csv",",",parse_dates=['Fecha'])

equis=lectura[["Tipo","Multa"]]
equis.head()

grp=equis.groupby("Multa")
grp.size()
my_plot = grp.plot(kind='bar')'



import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


lectura=pd.read_csv("dataset.csv",",",parse_dates=['Fecha'])

equis=lectura[["Tipo","Multa"]]
equis.head()

grp=equis.groupby("Multa")
grp.size()
my_plot = grp.plot(kind='bar')

In this link you can find the graphics that I have to make.

    
asked by Estudiante 23.08.2016 в 02:45
source

1 answer

4

When you do a grouping operation ( groupby ) you create a special object.

>>> print(type(grp))
<class 'pandas.core.groupby.DataFrameGroupBy'>

It is a kind of dictionary with information about the groups. What you seem to want to graph, and you have methods for it, are objects of data structures (e.g., Series , DataFrame , ...). When you apply an operation on pandas.core.groupby.DataFrameGroupBy , like when you apply the size method, it returns, for example in your case, a Series .

>>> print(type(grp.size()))
<class 'pandas.core.series.Series'>

If what you want to graph is the result of grp.size() you should do it like this:

>>> grp.size().plot(kind='bar')

And the result would be this:

If that is not what you want, please specify the question a little better and redo the answer.

    
answered by 23.08.2016 в 09:04