import pandas as pd
import pylab
TITANIC_CSV_URL="D:\\Python\\Titanic.csv"
INDEX_COL = 0
NAME_COL = "Name"
SEX_COL = "Sex"
PCLASS_COL= "PClass"
AGE_COL = "Age"
SURVIVED_COl = "Survived"
AGE_GROP_COL= "AgeGrop"
HEAD_ROWS = 15
df = pd.read_csv(TITANIC_CSV_URL,index_col=INDEX_COL)
df[AGE_GROP_COL]=df[AGE_COL]
df[AGE_GROP_COL].fillna(-1,inplace=True)
df[AGE_GROP_COL]=df[AGE_GROP_COL].map(lambda age: int(age//10)+1)
df[AGE_GROP_COL].value_counts()
age_by_survived = df.groupby([AGE_GROP_COL,SURVIVED_COl])
age_by_survived = age_by_survived.size().unstack()
age_by_survived_without_na = age_by_survived[1:].fillna(0)
def map_to_percent(r):
return [float(v)/sum(r) for v in r]
age_by_survived_without_na=age_by_survived_without_na.apply(map_to_percent,axis=1)
print(age_by_survived_without_na.plot(kand="barh",stacked=True))
pylab.show()
Это ошибка, которую я получаю:
Traceback (most recent call last):
File "D:\Python\big.py", line 32, in <module>
print(age_by_survived_without_na.plot(kand="barh",stacked=True))
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\plotting\_core.py", line 794, in __call__
return plot_backend.plot(data, kind=kind, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\plotting\_matplotlib\__init__.py", line 62, in plot
plot_obj.generate()
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 279, in generate
self._compute_plot_data()
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 414, in _compute_plot_data
raise TypeError("no numeric data to plot")
TypeError: no numeric data to plot
Я пытался
df=df.astype(float)
получил
File "D:\Python\big.py", line 31, in <module>
df=df.astype(float)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 5882, in astype
dtype=dtype, copy=copy, errors=errors, **kwargs
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\managers.py", line 581, in astype
return self.apply("astype", dtype=dtype, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\managers.py", line 438, in apply
applied = getattr(b, f)(**kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\blocks.py", line 559, in astype
return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals\blocks.py", line 643, in _astype
values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\dtypes\cast.py", line 729, in astype_nansafe
return arr.astype(dtype, copy=True)
ValueError: could not convert string to float: '1st'