2017-06-28 13 views
1

Versuchen Sie herauszufinden, warum Pandas ein Float zurückgibt, wenn das Datenfeld ein int ist. Gibt es einen Weg dahin? Ich versuche, einige CQL-Befehle auszugeben, und das bringt mich immer wieder durcheinander. DankePandas DataFrame dtype ist Int64 gibt Float64 zurück

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 

Antwort

1

Eine Reihe hat einen dtype. Ein Datenrahmen ist eine Sammlung von Reihen, wobei jede Spalte eine separate Reihe ist und ihren eigenen dtype hat. df.loc[0] packt eine Reihe. Diese Reihe war nicht eine Serie für sich. Pandas konvertiert es in eine Serie, muss aber nun einen Dtyp zuweisen. Da andere Elemente dieser Zeile float sind, wird der int aufwärts floated.

Verwandte Themen