使用熊猫比较两列

新手上路,请多包涵

以此为起点:

 a = [['10', '1.2', '4.2'], ['15', '70', '0.03'], ['8', '5', '0']]
df = pd.DataFrame(a, columns=['one', 'two', 'three'])

看起来像

  one  two three
0   10  1.2   4.2
1   15  70   0.03
2    8   5     0

我想在熊猫中使用类似 if 语句的内容。

 if df['one'] >= df['two'] and df['one'] <= df['three']:
    df['que'] = df['one']

基本上,通过 if 语句检查每一行来创建一个新列。

文档说要使用 .all 但没有例子……

原文由 Merlin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 228
2 个回答

你可以使用 np.where 。如果 cond 是布尔数组, AB 是数组,那么

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

defines C to be equal to A where cond is True, and B where cond is 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']),
    df['one'] < df['two']]

choices = [df['one'], df['two']]

df['que'] = np.select(conditions, choices, default=np.nan)

产量

  one  two three  que
0  10  1.2   4.2   10
1  15   70  0.03   70
2   8    5     0  NaN

如果我们可以假设 df['one'] >= df['two']df['one'] < df['two'] 为假时,那么条件和选择可以简化为

conditions = [
    df['one'] < df['two'],
    df['one'] <= df['three']]

choices = [df['two'], df['one']]

(如果 df['one']df['two'] 包含 NaN,假设可能不成立。)


注意

a = [['10', '1.2', '4.2'], ['15', '70', '0.03'], ['8', '5', '0']]
df = pd.DataFrame(a, columns=['one', 'two', 'three'])

定义一个带有字符串值的 DataFrame。由于它们看起来是数字,您最好将这些字符串转换为浮点数:

 df2 = df.astype(float)

然而,这会改变结果,因为字符串是逐个字符比较的,而浮点数是按数字比较的。

 In [61]: '10' <= '4.2'
Out[61]: True

In [62]: 10 <= 4.2
Out[62]: False

原文由 unutbu 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以将 .equals 用于列或整个数据帧。

 df['col1'].equals(df['col2'])

如果它们相等,则该语句将返回 True ,否则 False

原文由 ccook5760 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题