如何在Pandas DataFrame中将字符串转换为浮点数? |
您所在的位置:网站首页 › 无法将字符串转换为浮点数 › 如何在Pandas DataFrame中将字符串转换为浮点数? |
需要在 Pandas DataFrame 中将字符串转换为浮点数吗? 根据场景,你可以使用以下两种方法之一将字符串转换为 Pandas DataFrame 中的浮点数: (1) astype(float) df['DataFrame Column'] = df['DataFrame Column'].astype(float)(2) to_numeric df['DataFrame Column'] = pd.to_numeric(df['DataFrame Column'],errors='coerce')在这个简短的指南中,你将看到 3 个场景以及将字符串转换为浮点数的步骤和DataFrame字符串转换为浮点数的方法: 对于包含存储为字符串的数值的列对于同时包含数字和非数字值的列对于整个数据帧 在 Pandas DataFrame 中将字符串转换为浮点数的场景 场景 1:数值存储为字符串为了简单起见,让我们创建一个只有两列的 DataFrame: ProductPriceABC250XYZ270下面是在 Python 中创建 DataFrame的代码,其中 'Price' 列下的值存储为字符串(通过在这些值周围使用单引号。请注意,使用双引号将应用相同的概念): import pandas as pd data = {'Product': ['ABC','XYZ'], 'Price': ['250','270'] } df = pd.DataFrame(data) print (df) print (df.dtypes)在 Python 中运行代码,你将看到'Price' 列的数据类型是Object: Product Price 0 ABC 250 1 XYZ 270 Product object Price object dtype: object目标是将“价格”列下的值转换为浮点数。 然后,你可以使用 astype(float)方法执行到浮点数的转换: df['DataFrame Column'] = df['DataFrame Column'].astype(float)在我们示例的上下文中,“DataFrame Column”是“Price”列。因此,DataFrame字符串转换为浮点数示例: import pandas as pd data = {'Product': ['ABC','XYZ'], 'Price': ['250','270'] } df = pd.DataFrame(data) df['Price'] = df['Price'].astype(float) print (df) print (df.dtypes)你现在将看到“价格”列已转换为浮点数: Product Price 0 ABC 250.0 1 XYZ 270.0 Product object Price float64 dtype: object 场景 2:数字和非数字值让我们创建一个具有两列(“产品”和“价格”列)的新 DataFrame。只是这一次,“价格”列下的值将包含数字和非数字数据的组合: ProductPriceAAA250BBBABC260CCC270DDD280XYZ这是 DataFrame 在 Python 中的样子: import pandas as pd data = {'Product': ['AAA','BBB','CCC','DDD'], 'Price': ['250','ABC260','270','280XYZ'] } df = pd.DataFrame(data) print (df) print(df.dtypes)和以前一样,“价格”列的数据类型是对象: Product Price 0 AAA 250 1 BBB ABC260 2 CCC 270 3 DDD 280XYZ Product object Price object dtype: object然后,你可以使用 to_numeric方法将“价格”列下的值转换为浮点数: df['DataFrame Column'] = pd.to_numeric(df['DataFrame Column'], errors='coerce')通过设置 errors='coerce',你会将非数字值转换为NaN。 这是你可以使用的完整代码: import pandas as pd data = {'Product': ['AAA','BBB','CCC','DDD'], 'Price': ['250','ABC260','270','280XYZ'] } df = pd.DataFrame(data) df['Price'] = pd.to_numeric(df['Price'], errors='coerce') print (df) print(df.dtypes)运行代码,你将看到“价格”列现在是一个浮点数: Product Price 0 AAA 250.0 1 BBB NaN 2 CCC 270.0 3 DDD NaN Product object Price float64 dtype: object为了更进一步,你甚至可以使用df.replace将“NaN”值替换为“0”值: import pandas as pd import numpy as np data = {'Product': ['AAA','BBB','CCC','DDD'], 'Price': ['250','ABC260','270','280XYZ'] } df = pd.DataFrame(data) df ['Price'] = pd.to_numeric(df['Price'], errors='coerce') df = df.replace(np.nan, 0, regex=True) print (df) print(df.dtypes)这是你会得到的: Product Price 0 AAA 250.0 1 BBB 0.0 2 CCC 270.0 3 DDD 0.0 Product object Price float64 dtype: object 场景三:在整个DataFrame下将Strings转化为FloatsPandas DataFrame将字符串转换为浮点数:对于最后一个场景,让我们创建一个包含 3 列的 DataFrame,其中所有值都将存储为字符串(使用单引号): import pandas as pd data = {'Price_1': ['300','750','600','770','920'], 'Price_2': ['250','270','950','580','410'], 'Price_3': ['530','480','420','290','830'] } df = pd.DataFrame(data) print (df) print (df.dtypes)如你所见,DataFrame 中所有列的数据类型都是object: Price_1 Price_2 Price_3 0 300 250 530 1 750 270 480 2 600 950 420 3 770 580 290 4 920 410 830 Price_1 object Price_2 object Price_3 object dtype: objectDataFrame字符串转换为浮点数的方法 - 然后,你可以添加以下语法将所有值转换为整个DataFrame下的浮点数: df = df.astype(float)因此,执行转换的完整 Python 代码,DataFrame字符串转换为浮点数示例如下: import pandas as pd data = {'Price_1': ['300','750','600','770','920'], 'Price_2': ['250','270','950','580','410'], 'Price_3': ['530','480','420','290','830'] } df = pd.DataFrame(data) df = df.astype(float) print (df) print (df.dtypes)整个 DataFrame 下的所有列现在都是浮动的: Price_1 Price_2 Price_3 0 300.0 250.0 530.0 1 750.0 270.0 480.0 2 600.0 950.0 420.0 3 770.0 580.0 290.0 4 920.0 410.0 830.0 Price_1 float64 Price_2 float64 Price_3 float64 dtype: object你可能还需要查看以下指南以了解其他转换: 字符串到整数整数到浮点数整数到字符串 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |