关于数据转换问题,求大师帮忙,谢谢
data =[["162719","A","2022-12-20","8","6"],["004243","C","2022-12-19",'',"4"],["519212","B","2022-12-18","56","3"],["162411","E","2022-12-17","8","7"],
["007844","FC","2022-12-16","","8"],["163208","G","2022-12-15","66","12"]]
columns = ["编号","名称","日期","A","B"]
df2=pd.DataFrame(data,columns =columns)
df2['A'] = df2['A'].astype(float)
df2.loc, '编号'] = "ggg"
df2.loc, 'A'] = df2.head(3)['A'].sum()
print(df2)
A列为什么不能转换为float? 请大师帮忙,谢谢 本帖最后由 Threebody1 于 2023-4-17 16:10 编辑
在 data 列表中有一个空字符串(''),因此该列被推断为字符串类型。因此,在转换为浮点数之前,需要先处理该列中的空值和非数字值。
可以使用 pd.to_numeric() 函数将该列转换为数值类型,并通过 errors='coerce' 参数指定将无效数据(例如空字符串、字母等)转换为 NaN 值:
python
df2['A'] = pd.to_numeric(df2['A'], errors='coerce').astype(float)
此外,在执行 df2.loc, 'A'] = df2.head(3)['A'].sum() 这行代码之前,最好确保 A 列已经转换为 float 类型和处理了所有无效值 问题出在原始数据中的一些空字符串(''),这会导致在尝试将其转换为 float 时引发错误。解决这个问题的一个简单方法是在将 A 列转换为 float 类型之前,先将空字符串替换为 NaN 或特定的数字。这里,我们可以使用 Pandas 中的 replace 方法将空字符串替换为 np.nan(表示缺失值)或 0。以下是修改后的代码:
import pandas as pd
import numpy as np
data = [["162719","A","2022-12-20","8","6"],["004243","C","2022-12-19",'',"4"],
["519212","B","2022-12-18","56","3"],["162411","E","2022-12-17","8","7"],
["007844","FC","2022-12-16","","8"],["163208","G","2022-12-15","66","12"]]
columns = ["编号","名称","日期","A","B"]
df2 = pd.DataFrame(data, columns=columns)
# 将空字符串替换为 np.nan 或 0,根据需要进行选择
df2['A'] = df2['A'].replace('', np.nan)# 或者使用 df2['A'].replace('', 0)
# 然后将 A 列转换为 float 类型
df2['A'] = df2['A'].astype(float)
df2.loc, '编号'] = "ggg"
df2.loc, 'A'] = df2.head(3)['A'].sum()
print(df2)
这样,您应该能够在不引发错误的情况下将 A 列转换为 float 类型。
页:
[1]