如何让 pandas 附加一个整数并保留整数数据类型?我意识到我可以在输入数据后将 df.test.astype(int) 添加到整个列,但如果我可以在附加数据时执行此操作,那么这似乎是一个更好的方法。这是一个示例:
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
它将整数更改为浮点数。
这是因为您的初始数据框是空的。用一些整数列初始化它。
df = pd.DataFrame(dict(A=[], test=[], another=[]), dtype=int)
df.append(dict(A=3, test=4, another=5), ignore_index=True)
我做了吗
df = pd.DataFrame()
df.append(dict(A=3, test=4, another=5), ignore_index=True)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)