python怎么将两列数据比大小_python – 使用大pandas比较两列

您可以使用

np.where.如果cond是一个布尔数组,并且A和B是数组,那么

C = np.where(cond, A, B)

定义C等于A,其中cond为True,而B为cond,则为False.

import numpy as np

import pandas as pd

a = [['10', '1.2', '4.2'], ['15', '70', '0.03'], ['8', '5', '0']]

df = pd.DataFrame(a, columns=['one', 'two', 'three'])

df['que'] = np.where((df['one'] >= df['two']) & (df['one'] <= df['three'])

, df['one'], np.nan)

产量

one two three que

0 10 1.2 4.2 10

1 15 70 0.03 NaN

2 8 5 0 NaN

如果您有多个条件,则可以使用np.select.

例如,如果您希望df [‘que’]等于df [‘two’],当df [‘one’]< df ['two'],然后

conditions = [

(df['one'] >= df['two']) & (df['one'] <= df['three']),

你可能感兴趣的:(python怎么将两列数据比大小_python – 使用大pandas比较两列)