2017-06-28 21 views
1

データフィールドがintのときにPandasが浮動小数点を返す理由を理解しようとします。これを回避する方法はありますか?いくつかのCQLコマンドを出力しようとすると、これは私を混乱させ続けます。ありがとうPandas DataFrame dtypeがInt64である場合Float64

df = pd.DataFrame([[11001, 28154, 2457146.7149722599, 37.070666000000003], 
[110, 28154, 2457146.7149722599, 37.070666000000003], 
[1100, 28154, 2457146.7149722599, 37.070666000000003], 
[110, 28, 2457146.7149722599, 37.070666000000003]]) 
print("\nNote: the first two fields are int64") 
print(df.dtypes) 
print("\nPrinting the first record of the first field returns an int... GOOD!") 
print(df.iloc[0,0]) 
print("\nSaving the first row off and printing the first fields data returns a float... BAD!") 
row1 = df.iloc[0] 
print(row1[0]) 

Note: the first two fields are int64 
0  int64 
1  int64 
2 float64 
3 float64 
dtype: object 

Printing the first record of the first field returns an int... GOOD! 
11001 

Saving the first row off and printing the first fields data returns a float... BAD! 
11001.0 

答えて

1

シリーズにはdtypeがあります。データフレームは、各列が別々の系列であり、独自のdtypeを持つ一連の系列です。 df.loc[0]行を取得します。この列はではありませんでした。は単独でシリーズです。パンダはそれをシリーズに変換しますが、今はdtypeを割り当てなければなりません。この行の他の要素は浮動小数点型だったので、intは浮動小数点数になり浮動小数点になります。

関連する問題