2017-06-28 49 views
1

試圖找出Pandas爲什麼在數據字段是int時返回一個浮點數。有沒有解決的辦法?試圖輸出一些CQL命令,這讓我不知所措。謝謝熊貓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被上移到浮動狀態。