Replacing values greater than a number in pandas dataframe

前端 未结 4 2011
甜味超标
甜味超标 2021-01-01 09:20

I have a large dataframe which looks as:

df1[\'A\'].ix[1:3]
2017-01-01 02:00:00    [33, 34, 39]
2017-01-01 03:00:00    [3, 43, 9]

I want to

相关标签:
4条回答
  • 2021-01-01 10:06

    Very simply : df[df > 9] = 11

    0 讨论(0)
  • 2021-01-01 10:10

    You can use apply with list comprehension:

    df1['A'] = df1['A'].apply(lambda x: [y if y <= 9 else 11 for y in x])
    print (df1)
                                    A
    2017-01-01 02:00:00  [11, 11, 11]
    2017-01-01 03:00:00    [3, 11, 9]
    

    Faster solution is first convert to numpy array and then use numpy.where:

    a = np.array(df1['A'].values.tolist())
    print (a)
    [[33 34 39]
     [ 3 43  9]]
    
    df1['A'] = np.where(a > 9, 11, a).tolist()
    print (df1)
                                    A
    2017-01-01 02:00:00  [11, 11, 11]
    2017-01-01 03:00:00    [3, 11, 9]
    
    0 讨论(0)
  • 2021-01-01 10:12

    I came for a solution to replacing each element larger than h by 1 else 0, which has the simple solution:

    df = (df > h) * 1
    

    (This does not solve the OP's question as all df <= h are replaced by 0.)

    0 讨论(0)
  • 2021-01-01 10:17

    You can use numpy indexing, accessed through the .values function.

    df['col'].values[df['col'].values > x] = y

    where you are replacing any value greater than x with the value of y.

    So for the example in the question:

    df1['A'].values[df1['A'] > 9] = 11

    0 讨论(0)
提交回复
热议问题