如何在不使用循环的情况下将Pandas字符串列转换为特定的数字?

qgelzfjb  于 2023-04-10  发布在  其他
关注(0)|答案(2)|浏览(127)

我有一列字符串,我想转换成特定的数字。我目前的方法涉及使用for循环,但我觉得这不是Pandas的设计用途。有人能建议一个更优雅的解决方案,适用于多个列吗?
下面是我的代码-

import pandas as pd
data = [['mechanical@engineer', 'field engineer'], ['field engineer', 'lab_scientist'],
        ['lab_scientist', 'mechanical@engineer'], ['field engineer', 'mechanical@engineer'],
        ['lab_scientist','mechanical@engineer']]# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Job1', 'Job2'])
for index, row in df.iterrows():
    if row['Job1']=="mechanical@engineer":
        row['Job1'] = 0
    elif row['Job1']=="field engineer":
        row['Job1'] = 1
    elif row['Job1'] == "lab_scientist":
        row['Job1'] = 2
print(df.head())
ppcbkaq5

ppcbkaq51#

你只需要一张Map:

role_to_code = {"mechanical@engineer": 0, "field engineer": 1, "lab_scientist": 2}

df.Job1.map(role_to_code)
#0    0
#1    1
#2    2
#3    1
#4    2
#Name: Job1, dtype: int64
u0sqgete

u0sqgete2#

为什么不使用replace函数而不是for循环?

mapping = {'mechanical@engineer': 0, 'field engineer': 1, 'lab_scientist': 2}

df = df.replace(mapping)

print(df.head())

输出将是:

Job1  Job2
0     0     1
1     1     2
2     2     0
3     1     0
4     2     0

相关问题