2016-11-09 4 views
3

をint型したいですか?私はデータを入れた後、列全体にdf.test.astype(int)を渡すことができますが、データを追加しているときにそれを行うことができれば、より良い方法になるようです。ここではサンプルです:ここではアペンドパンダのデータフレームが自動的にfloat型としてキャストが、私は整数を追加し、整数データ型を保つためにパンダを取得するにはどうすればよい

from bitstring import BitArray 
import pandas as pd 
df = pd.DataFrame() 

test = BitArray('0x01') 
test = int(test.hex) 
print(test) 
df = df.append({'test':test, 'another':5}, ignore_index=True) 

print(df.test) 
print(df.another) 

が出力されます。

1 
0 1.0 
Name: test, dtype: float64 
0 5.0 
Name: another, dtype: float64 

それは山車に整数を変更しています。

答えて

3

初期データフレームが空であることが原因です。整数列で初期化してください。

df = pd.DataFrame(dict(A=[], test=[], another=[]), dtype=int) 
df.append(dict(A=3, test=4, another=5), ignore_index=True) 

enter image description here


私は、列ごとに異なるdtypesを行うことが可能である

df = pd.DataFrame() 
df.append(dict(A=3, test=4, another=5), ignore_index=True) 

enter image description here

+0

を行っていましたか? – kaminsknator

関連する問題