简体   繁体   English

如何更改Pandas中除第一列之外的所有列的列类型?

[英]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.

相关问题 如何删除除前三列之外所有列值为空的pandas中的行? - How to delete the rows in pandas where all the column values are null except first three columns? 熊猫根据第一列以外的所有现有列的值创建新列 - Pandas create new columns base on all existing columns' values, except the first column 如何增加 header 除了 Pandas 中的第一列 - How to increment header except the first column in Pandas 如何在熊猫中选择除一列之外的所有列? - How to select all columns except one in pandas? 按除第一列以外的所有列分组,但聚合为列出第一列 - Group by the all the columns except the first one, but aggregate as list the first column Pandas Groupby 除了列并取该列的第一个值 - Pandas Groupby except column and take that columns first value 如何使用除第一个包含名称的列之外的所有列的 pandas 计算 python 中的累积总和? - How to calculate cumulative sum in python using pandas of all the columns except the first one that contain names? 有没有办法替换除熊猫数据框列中值的前 3 个实例之外的所有值 - Is there a way to replace all except the first 3 instances of a value in a pandas dataframe column 如何缩放除最后一列之外的所有列? - How to scale all columns except last column? Pandas.删除除 1 列之外的所有缺失值的列 - Pandas.Drop all columns with missing values except 1 column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM