새로새록

TypeError: no numeric data to plot [ pandas object(콤마있는 문자열) int(숫자)로 전환하기 ] 본문

글/리뷰

TypeError: no numeric data to plot [ pandas object(콤마있는 문자열) int(숫자)로 전환하기 ]

류지나 2021. 9. 13. 01:22
TypeError: no numeric data to plot

 [ pandas object(콤마있는 문자열) int(숫자)로 전환하기 ]


df['금액']=df['금액'].str.replace(pat=r'[^A-Za-z0-9]', repl='', regex=True) #"," 제거
df['금액'] = pd.to_numaeric(df['금액']) #int type전환

df['금액'].dtypes

ex.일인당 국민총소등소과 증가율

df_gdf = pd.read_excel('gdp.xls')
names = list(df_gdf.loc[1, :])
df_gdf.columns=names
df_gdf.dropna(inplace=True)
df_gdf.reset_index(inplace=True)

파일 가져올때 ()안에 thousands=',' 처리하면 되나 인덱스도 바뀌어서 딴 걸로 해봄.

 

 

df_gdf.loc[[0,1]] 혹은

df_gdf.iloc[[0,1]]

df_gdf.iloc[[0,1],:]

2 1인당 실질 국민총소득(만 원) 2,331 2,383 2,465 2,592 2,582 2,633 2,808 2,832 2,900 2,998 3,083 3,260 3,391 3,493 3,531 3,536 3,523
3 전년 대비 증가율(%) 3.9 2.2 3.4 5.2 -0.4 2.0 6.7 0.8 2.4 3.4 2.8 5.8 4.0 3.0 1.1 0.2 -0.4

 

 

 

gdp_series = df_gdf.loc[0, '2004':'2020'].str.replace(pat=r'[^\w]', repl=r'', regex=True)

gdp_series

#이래도 문자만 제거될뿐, 여전히 object

Out[120]:

2004 2331

2005 2383

2006 2465

2007 2592

2008 2582

2009 2633

2010 2808

2011 2832

2012 2900

2013 2998

2014 3083

2015 3260

2016 3391

2017 3493

2018 3531

2019 3536

2020 3523

Name: 0, dtype: object

 

 

 

gdp_series = pd.to_numeric(gdp_series)gdp_series

# 이제야 int

 

gdp_series.plot()