我正在从文件夹中读取一些.csv
文件。我正在尝试通过使用每个文件来创建数据帧列表。
在某些文件中,列值(即Quantity
为str
和float64
数据类型。因此,我正在尝试将该列quantity
转换为int
。
我正在使用其位置/索引访问列(出于自动化目的)。
列表中所有数据帧中的一个,
CustName ProductID Quantity
0 56MED 110 '1215.0'
1 56MED 112 5003.0
2 56MED 114 '6822.0'
3 WillSup 2285 5645.0
4 WillSup 5622 6523.0
5 HammSup 9522 1254.0
6 HammSup 6954 5642.0
因此,我的表情是这样的
df.columns[2] = pd.to_numeric(df.columns[2], errors='coerce').astype(str).astype(np.int64)
我得到了
TypeError:索引不支持可变操作
在此之前,我尝试过
df.columns[2] = pd.to_numeric(df.columns[2], errors='coerce').fillna(0).astype(str).astype(np.int64)
但是,我遇到了这个错误,
AttributeError:“ numpy.float64”对象没有属性“ fillna”
有些帖子直接使用列名,而不使用列位置。如何使用int
中的列位置/索引将列转换为pnadas
?
我的pandas
版本
print(pd.__version__)
>> 0.23.3
答案 0 :(得分:2)
df.columns[2]
返回一个标量,在这种情况下为字符串。
要访问系列,请使用df['Quantity']
或df.iloc[:, 2]
甚至是df[df.columns[2]]
。如果您确定自己的数据应该是整数,则可以使用downcast='integer'
。而不是重复的转换。
所有这些都是等效的:
df['Quantity'] = pd.to_numeric(df['Quantity'], errors='coerce', downcast='integer')
df.iloc[:, 2] = pd.to_numeric(df.iloc[:, 2], errors='coerce', downcast='integer')
df[df.columns[2]] = pd.to_numeric(df[df.columns[2]], errors='coerce', downcast='integer')
答案 1 :(得分:1)
尝试一下,您需要先从字符串中删除那些引号,然后再使用pd.to_numeric
:
df.iloc[:, 2] = pd.to_numeric(df.iloc[:, 2].str.strip('\'')).astype(int)
或来自@jpp:
df['Quantity'] = pd.to_numeric(df['Quantity'].str.strip('\''), errors='coerce', downcast='integer')
输出,df.info():
<class 'pandas.core.frame.DataFrame'>
Int64Index: 7 entries, 0 to 6
Data columns (total 3 columns):
CustName 7 non-null object
ProductID 7 non-null int64
Quantity 7 non-null int32
dtypes: int32(1), int64(1), object(1)
memory usage: 196.0+ bytes
输出:
CustName ProductID Quantity
0 56MED 110 1215
1 56MED 112 5003
2 56MED 114 6822
3 WillSup 2285 5645
4 WillSup 5622 6523
5 HammSup 9522 1254
6 HammSup 6954 5642