Pandas:我怎样才能改变一列中元素的类型?

2024-09-26 22:52:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pymongo从mongo数据库创建了一个pandas数据帧。在数据库中,列中的元素看起来像整数,但当我创建dataframe时,它们是浮点数。我希望它们是整数。我怎么能做到呢?我只想换个专栏

尝试

X = X.astype({'x':'int'})
myclient = pymongo.MongoClient("mongodb+srv://bla-bla")
mydb = myclient["p"]

mycol = mydb["aDB"]

colnames={ "_id": 1,"x":1, "y":1,"z":1}
X = pd.DataFrame(list(mycol.find({},colnames)))

Tags: 数据数据库元素dataframepandasmongo整数浮点数

热门问题