Python - 将Dataframe中的所有项目转换为字符串 [英] Python - Turn all items in a Dataframe to strings

查看:15295
本文介绍了Python - 将Dataframe中的所有项目转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循以下程序:在Python中,如何将列表中的所有项目转换为浮点?因为我的Dataframe的每一列都是 list 而不是 float 我选择将所有值更改为 string



df = [str(i)for d in df]



但这失败了/ p>

它只是删除除了第一行列名之外的所有数据。



然后,尝试 df = [str(i)for d in df.values] 导致将整个Dataframe更改为一个大的列表,但是这些数据太多了,无法达到目标我的脚本是将Dataframe导出到我的Oracle表。



有没有办法将我的数据框中没有字符串的所有项目转换成字符串?

解决方案

您可以使用 applymap 方法:

  df = df.applymap(str)


I followed the following procedure: In Python, how do I convert all of the items in a list to floats? because each column of my Dataframe is list, but instead of floats I chose to change all the values to strings.

df = [str(i) for i in df]

But this failed.

It simply erased all the data except for the first row of column names.

Then, trying df = [str(i) for i in df.values] resulted in changing the entire Dataframe into one big list, but that messes up the data too much to be able to meet the goal of my script which is to export the Dataframe to my Oracle table.

Is there a way to convert all the items that are in my Dataframe that are NOT strings into strings?

解决方案

You can use applymap method:

df = df.applymap(str)

这篇关于Python - 将Dataframe中的所有项目转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆