python-在Pandas数据框中将值(例如,使用性别)从字符串映射到int

这个问题已经在这里有了答案:            >            Replacing column values in a pandas DataFrame                                    10个
我有一个名为df_base的数据框,看起来像这样.如您所见,有一列名为“性别”的性别.我想将这些值分别映射到0和1.

+---+-------------+----------+--------+---------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+
|   | PassengerId | Survived | Pclass |                       Name                        |  Sex   | Age | SibSp | Parch |      Ticket      |  Fare   | Cabin | Embarked |
+---+-------------+----------+--------+---------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+
| 0 |           1 |        0 |      3 | Braund, Mr. Owen Harris                           | male   |  22 |     1 |     0 | A/5 21171        |    7.25 | NaN   | S        |
| 1 |           2 |        1 |      1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female |  38 |     1 |     0 | PC 17599         | 71.2833 | C85   | C        |
| 2 |           3 |        1 |      3 | Heikkinen, Miss. Laina                            | female |  26 |     0 |     0 | STON/O2. 3101282 |   7.925 | NaN   | S        |
| 3 |           4 |        1 |      1 | Futrelle, Mrs. Jacques Heath (Lily May Peel)      | female |  35 |     1 |     0 | 113803           |    53.1 | C123  | S        |
| 4 |           5 |        0 |      3 | Allen, Mr. William Henry                          | male   |  35 |     0 |     0 | 373450           |    8.05 | NaN   | S        |
+---+-------------+----------+--------+---------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+

我在*上看到了一些方法,但是我想知道执行以下映射最有效的方法是:

+---------+---------+
| Old Sex | New Sex |
+---------+---------+
| male    |       0 |
| female  |       1 |
| female  |       1 |
| female  |       1 |
| male    |       0 |
+---------+---------+

我正在使用这个:

df_base [ ‘性别’].代替([ ‘男性’, ‘女’],[0,1],就地=真)

…但是我不禁觉得这有点伪劣.有更好的方法吗?还使用了.loc但它在Dataframe的行周围循环,所以效率较低,对吧?

解决方法:

我认为,如果“性别”列中仅存在男性和女性,则按字典最好/更快地使用map

df_base['Sex'] = df_base['Sex'].map(dict(zip(['male','female'],[0,1]))

像什么一样:

df_base['Sex'] = df_base['Sex'].map({'male': 0,'female': 1})

如果仅存在女性和男性值,则将布尔值掩码强制转换为整数True / False到1,0:

df_base['Sex'] = (df_base['Sex'] == 'female').astype(int)

性能:

np.random.seed(2019)

import perfplot    

def ma(df):
    df = df.copy()
    df['Sex_new'] = df['Sex'].map({'male': 0,'female': 1})
    return df

def rep1(df):
    df = df.copy()
    df['Sex'] = df['Sex'].replace(['male','female'],[0,1])
    return df

def nwhere(df):
    df = df.copy()
    df['Sex_new'] = np.where(df['Sex'] == 'male', 0, 1)
    return df

def mask1(df):
    df = df.copy()
    df['Sex_new'] = (df['Sex'] == 'female').astype(int)
    return df

def mask2(df):
    df = df.copy()
    df['Sex_new'] = (df['Sex'].values == 'female').astype(int)
    return df


def make_df(n):
    df = pd.DataFrame({'Sex': np.random.choice(['male','female'], size=n)})

    return df
perfplot.show(
    setup=make_df,
    kernels=[ma,  rep1, nwhere, mask1, mask2],
    n_range=[2**k for k in range(2, 18)],
    logx=True,
    logy=True,
    equality_check=False,  # rows may appear in different order
    xlabel='len(df)')

python-在Pandas数据框中将值(例如,使用性别)从字符串映射到int

结论:

如果仅替换2个值是最慢的替换,则numpy.where,map和mask相似.为了提高性能,请使用numpy数组与.values进行比较.
同样,所有数据都取决于数据,因此最好对真实数据进行测试.

上一篇:友好的网址映射问题-Java Spring


下一篇:Django项目连接多个数据库配置