pandas 将字符串转换为数字类型
本教程解释了如何使用 pandas.to_numeric()
方法将 pandas dataframe 的字符串值转换为数字类型。
import pandas as pd
items_df = pd.dataframe(
{
"id": [302, 504, 708, 103, 343, 565],
"name": ["watch", "camera", "phone", "shoes", "laptop", "bed"],
"cost": ["300", "400", "350", "100", "1000", "400"],
}
)
print(items_df)
输出:
id name cost
0 302 watch 300
1 504 camera 400
2 708 phone 350
3 103 shoes 100
4 343 laptop 1000
5 565 bed 400
我们将用上面的例子来演示如何将 dataframe 的值转换为数字类型。
pandas.to_numeric()
方法
语法
pandas.to_numeric(arg, errors="raise", downcast=none)
它将作为 arg
传递的参数转换为数字类型。默认情况下,arg
将被转换为 int64
或 float64
。我们可以设置 downcast
参数的值,将 arg
转换为其他数据类型。
使用 pandas.to_numeric()
方法将 pandas dataframe 的字符串值转换为数字类型
import pandas as pd
items_df = pd.dataframe(
{
"id": [302, 504, 708, 103, 343, 565],
"name": ["watch", "camera", "phone", "shoes", "laptop", "bed"],
"cost": ["300", "400", "350", "100", "1000", "400"],
}
)
print("the items dataframe is:")
print(items_df, "\n")
print("datatype of cost column before type conversion:")
print(items_df["cost"].dtypes, "\n")
items_df["cost"] = pd.to_numeric(items_df["cost"])
print("datatype of cost column after type conversion:")
print(items_df["cost"].dtypes)
输出:
the items dataframe is:
id name cost
0 302 watch 300
1 504 camera 400
2 708 phone 350
3 103 shoes 100
4 343 laptop 1000
5 565 bed 400
datatype of cost column before type conversion:
object
datatype of cost column after type conversion:
int64
它将 items_df
中 cost
列的数据类型从 object
转换为 int64
。
将 pandas dataframe 中的字符串值转换为含有其他字符的数字类型
如果我们想将一列转换成数值类型,其中有一些字符的值,我们得到一个错误说 valueerror: unable to parse string
。在这种情况下,我们可以删除所有非数字字符,然后进行类型转换。
import pandas as pd
items_df = pd.dataframe(
{
"id": [302, 504, 708, 103, 343, 565],
"name": ["watch", "camera", "phone", "shoes", "laptop", "bed"],
"cost": ["$300", "$400", "$350", "$100", "$1000", "$400"],
}
)
print("the items dataframe is:")
print(items_df, "\n")
print("datatype of cost column before type conversion:")
print(items_df["cost"].dtypes, "\n")
items_df["cost"] = pd.to_numeric(items_df["cost"].str.replace("$", ""))
print("datatype of cost column after type conversion:")
print(items_df["cost"].dtypes, "\n")
print("dataframe after type conversion:")
print(items_df)
输出:
the items dataframe is:
id name cost
0 302 watch $300
1 504 camera $400
2 708 phone $350
3 103 shoes $100
4 343 laptop $1000
5 565 bed $400
datatype of cost column before type conversion:
object
datatype of cost column after type conversion:
int64
dataframe after type conversion:
id name cost
0 302 watch 300
1 504 camera 400
2 708 phone 350
3 103 shoes 100
4 343 laptop 1000
5 565 bed 400
它删除了与 cost
列的值相连的 $
字符,然后使用 pandas.to_numeric()
方法将这些值转换为数字类型。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串