[英]How to change the column type of all columns except the first in Pandas?
I have a 6,000 column table that is loaded into a pandas DataFrame.我有一个 6,000 列的表,它被加载到 pandas DataFrame 中。 The first column is an ID, the rest are numeric variables.
第一列是一个 ID,rest 是数字变量。 All the columns are currently strings and I need to convert all but the first column to integer.
所有列当前都是字符串,我需要将除第一列之外的所有列转换为 integer。
Many of the functions I've found don't allow passing a list of column names or drop the first column entirely.我发现的许多函数不允许传递列名列表或完全删除第一列。
You can do:你可以做:
df.astype({col: int for col in df.columns[1:]})
An easy trick when you want to perform an operation on all columns but a few is to set the columns to ignore as index:当您想对所有列执行操作时,一个简单的技巧是将列设置为忽略为索引:
ignore = ['col1']
df = (df.set_index(ignore, append=True)
.astype(float)
.reset_index(ignore)
)
This should work with any operation even if it doesn't support specifying on which columns to work.这应该适用于任何操作,即使它不支持指定要在哪些列上工作。
Example input:示例输入:
df = pd.DataFrame({'col1': list('ABC'),
'col2': list('123'),
'col3': list('456'),
})
output: output:
>>> df.dtypes
col1 object
col2 float64
col3 float64
dtype: object
Try something like:尝试类似:
df.loc[:, df.columns != 'ID'].astype(int)
Some code that could be used for general cases where you want to convert dtypes
一些可用于您想要转换
dtypes
的一般情况的代码
# select columns that need to be converted
cols = df.select_dtypes(include=['float64']).columns.to_list()
cols = ... # here exclude certain columns in cols e.g. the first col
df = df.astype({col:int for col in cols})
You can select str
columns and exclude the first column in your case.您可以 select
str
列并在您的情况下排除第一列。 The idea is basically the same.思路基本相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.