【问题标题】:How to replace elements within a pandas dataframe column according to an ordered list?如何根据有序列表替换熊猫数据框列中的元素?
【发布时间】:2019-02-12 06:27:09
【问题描述】:

假设我有这个熊猫数据框:

index  a        b
1    'pika'   'dog'
2    'halo'   'cat'
3    'polo'   'dog'
4    'boat'   'man'
5    'moan'   'tan'
6    'nope'   'dog'

我有一个这样的列表:

colors = ['black' , 'green', 'yellow']

如何将b 列中的所有dog 替换为元素

colors列表中以相同的顺序

基本上,我希望它看起来像这样:

index  a        b
1    'pika'  'black'
2    'halo'   'cat'
3    'polo'  'green'
4    'boat'   'man'
5    'moan'   'tan'
6    'nope'  'yellow'

【问题讨论】:

  • 如果有第四条“狗”,你希望它变成什么? “黑色”、“狗”还是 NaN?
  • 不会有第四只狗了。狗的长度将与颜色完全相同

标签: python pandas list dataframe append


【解决方案1】:

使用pd.DataFrame.loc 和布尔索引:

df.loc[df['b'].eq('dog'), 'b'] = colors

print(df)

   index     a       b
0      1  pika   black
1      2  halo     cat
2      3  polo   green
3      4  boat     man
4      5  moan     tan
5      6  nope  yellow

【讨论】:

  • 很好,很简单 :) 过度思考了这个过程,+1
【解决方案2】:

使用itertools.cycledf.applylambda

In [100]: import itertools as it

In [101]: colors_gen = it.cycle(colors)

In [102]: df1['c'] = df1['b'].apply(lambda x: next(colors_gen) if x == 'dog' else x)

In [103]: df1
Out[103]:
      a    b       c
0  pika  dog   black
1  halo  cat     cat
2  polo  dog   green
3  boat  man     man
4  moan  tan     tan
5  nope  dog  yellow

这也适用于更大的DataFrames

In [104]: df2 = pd.DataFrame({'a': ['pika', 'halo', 'polo', 'boat','moan','nope','etc','etc'], 'b':['dog','cat','dog','man','tan','dog','dog','dog']})

In [106]: df2['c'] = df2['b'].apply(lambda x: next(colors_gen) if x == 'dog' else x)

In [107]: df2
Out[107]:
      a    b       c
0  pika  dog   black
1  halo  cat     cat
2  polo  dog   green
3  boat  man     man
4  moan  tan     tan
5  nope  dog  yellow
6   etc  dog   black
7   etc  dog   green

【讨论】:

    【解决方案3】:

    使用 numpy put 的另一种方式

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({'a': ['pika', 'halo', 'polo', 'boat', 'moan', 'nope'],
                       'b': ['dog', 'cat', 'dog', 'man', 'tan', 'dog']})
    colors = ['black' , 'green', 'yellow']
    

    df

        a       b
    0   pika    dog
    1   halo    cat
    2   polo    dog
    3   boat    man
    4   moan    tan
    5   nope    dog
    

    -

    # 'wrap' mode is not needed when replacement list is same
    # size as the number of target values
    np.put(df.b, np.where(df.b == 'dog')[0], colors, mode='wrap')
    

    df

        a       b
    0   pika    black
    1   halo    cat
    2   polo    green
    3   boat    man
    4   moan    tan
    5   nope    yellow
    

    【讨论】:

      【解决方案4】:

      你可以检查

      n=(df.b=="'dog'").sum()
      
      df.loc[df.b=="'dog'",'b']=(['black' , 'green', 'yellow']*(n//3))[:n]
      

      【讨论】:

        猜你喜欢
        • 2018-10-23
        • 2020-10-21
        • 1970-01-01
        • 2019-05-21
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 2019-12-02
        • 2018-01-01
        相关资源
        最近更新 更多