2017-04-14 2 views
0

일련의 반올림 dataframe 2 곳으로팬더 내가 같은 팬더 시리즈가

23.340000 
24.350000 
...... 

내가 float로 변환하고 싶습니다를 다음 라운드

tempDF['A'] = round(dfOld.cl.astype(float),2) 

출력 같아야

23.34 
24.35 
..... 

가장 좋은 방법은 무엇입니까?

+2

를, "tempDF [ 'A'] = 라운드 (dfOld.cl.astype (플로트) , 2) "? 해당 코드가 올바른 것 같습니다. –

답변

1

당신이 달성하기 pandas.Series.round 기능을 사용할 수 있습니다 : 당신은 당신이 제공 한 코드를 실행할 때 무엇을 어떻게해야합니까

In [1]: import pandas as pd 

In [2]: dfOld = pd.DataFrame({'cl': ['0.014', '0.015', '0.016']}) 
dfOld.cl.apply(type).value_counts() # prove that the values are strings 

Out[2]: 
<type 'str'> 3 
dtype: int64 

In [3]: 
dfOld.cl.astype(float).round(decimals=2) 

Out[3]: 
0 0.01 
1 0.02 
2 0.02 
Name: cl, dtype: float64 
+0

덧붙여 말하면,'jupyter-notebook'을 사용하고 있다면'df.cl. '를 사용하면'Series'에 바인딩 된 모든 메소드를 나열하는 팝업을 얻을 수 있으며'dfOld.cl.round? '명령어가 포함 된 셀을 실행하여 메소드 서명을 볼 수 있습니다. – kjfl