2017-05-09 67 views
0

我有以下系列我從read_html獲得:爲什麼我不能更改系列格式?

series: 
1        417.951 
2        621.710 
3        164.042 
4        189.963 
5        555.123 
6        213.494 
7      2.873.093 

我想刪除.,以適用於一些功能,該列中的數字。

因此所需的輸出將是:

series: 
1        417951 
2        621710 
3        164042 
4        189963 
5        555123 
6        213494 
7       2873093 

我已經嘗試了更換recieving相同的結果:

df.replace('.','') 

,把該系列的數據幀,看看是否真的是問題,但它不斷返回最初的系列。

+1

您需要分配回'DF = df.replace'或通過PARAM'就地= TRUE;什麼是最終的所需的dtype在這裏?字符串或數字? – EdChum

+0

我不介意這種類型(無論是系列還是DF),因爲我會將它附加到數據框中 – ge00rge

+0

如果你想要數字,那麼你可以做'df.replace(','m'')。astype(int )'大多數熊貓操作都會返回一個副本,幾乎所有的方法都包含arg'inplace' – EdChum

回答

1

您需要分配輸出到Series並在必要時轉換爲int,而且是必要的逃生.通過\Series.replace添加參數regex

series = series.replace('\.','', regex=True) 
print (series) 
1  417951 
2  621710 
3  164042 
4  189963 
5  555123 
6  213494 
7 2873093 
Name: a, dtype: object 

series = series.replace('\.','', regex=True).astype(int) 
print (series) 
1  417951 
2  621710 
3  164042 
4  189963 
5  555123 
6  213494 
7 2873093 
Name: a, dtype: int32 

另一種解決方案是使用str.replace

series = series.str.replace('.','') 
print (series) 
1  417951 
2  621710 
3  164042 
4  189963 
5  555123 
6  213494 
7 2873093 
Name: a, dtype: object 

但尤爲明顯的是使用thousands參數read_html:( '', '')

df = pd.read_html(url, thousands='.') 
相關問題