【问题标题】:Delete specific rows based in conditions on rows from a dataframe pandas根据数据框 pandas 中行的条件删除特定行
【发布时间】:2022-02-08 20:53:48
【问题描述】:

我想根据 Pandas 数据框中行的条件删除特定行。

例如,由于我同时有几个货币对,我打算只选择同一时间的一种货币。

这是优先级:欧元、美元、英镑、瑞士法郎。

currency    timebuy buyprice
CNHUSD  2021-01-05 08:30:00 0,00005073
CNHGBP  2021-01-05 08:30:00 1,588
ZARGBP  2021-01-07 05:15:00 0,2727
ZARUSD  2021-01-07 05:15:00 300
ZAREUR  2021-01-07 13:00:00 0,1936
ZARCHF  2021-01-07 13:00:00 0,0000052
JPYCHF  2021-01-13 06:00:00 0,0002222
JPYUSD  2021-01-13 06:00:00 8
JPYGBP  2021-01-13 06:00:00 8

到

currency    timebuy buyprice
CNHUSD  2021-01-05 08:30:00 0,00005073
ZAREUR  2021-01-07 13:00:00 0,1936
JPYUSD  2021-01-13 06:00:00 8

【问题讨论】:

  • 您好,Fábio :) 请您在问题中以文本形式发布您的表格吗?我无法从图像中复制/粘贴。
  • 抱歉有错误,现在改正了!谢谢理查德克
  • 那么这里的逻辑是什么?
  • 我在同一个时间戳有几个货币对。我只想要每个时间戳中的一个。它因为 *EUR 是我的首选,如果不是我更喜欢 *USD、GDP、CHF...
  • 输出是否还应包括ZARUSD, 2021-01-07 05:15:00, 300 的行?

标签: python pandas


【解决方案1】:

使用groupby 和reindex:

# Hard-code your priority for the second currency in each pair
pri = ['EUR', 'USD', 'GBP', 'CHF']

# Create a new column for the second currency of each pair
df['2ndcurr'] = df['currency'].str[-3:]


# Group by time and second currency,
# Sort inner level (1) of resulting MultiIndex to match priority,
# Group by the outer level (0),
# Get the first row of each group,
# Reset timebuy from index into its own column

(df.set_index(['timebuy', '2ndcurr'])
   .reindex(pri, level=1)
   .groupby(level=0)
   .first()
   .reset_index())

               timebuy currency    buyprice
0  2021-01-05 08:30:00   CNHUSD  0,00005073
1  2021-01-07 05:15:00   ZARUSD         300
2  2021-01-07 13:00:00   ZAREUR      0,1936
3  2021-01-13 06:00:00   JPYUSD           8

【讨论】:

    【解决方案2】:

    对于这样的优先级列表,使用数字是最容易的。因此,您可以从优先级列表中创建一个不错的数字映射,并使用它来选择行:

    priority = ['EUR', 'USD', 'GBP', 'CHF']
    mapping = {p: i for i, p in enumerate(priority)}
    indexes = df['currency'].str[-3:].map(mapping).groupby(df['currency'].str[:3]).idxmin().sort_values()
    selected = df.loc[indexes]
    

    输出:

    >>> selected
      currency             timebuy    buyprice
    0   CNHUSD 2021-01-05 08:30:00  0,00005073
    4   ZAREUR 2021-01-07 13:00:00      0,1936
    7   JPYUSD 2021-01-13 06:00:00           8
    

    单线:

    priority = ['EUR', 'USD', 'GBP', 'CHF']
    filtered = df.loc[df['currency'].str[-3:].map({p: i for i, p in enumerate(priority)}).groupby(df['currency'].str[:3]).idxmin().sort_values()]
    

    如果您想按每个时间戳而不是currency 的前三个字母进行分组,请按df['timestamp'] 而不是df['currency'].str[:3] 进行分组,即:

    indexes = df['currency'].str[-3:].map(mapping).groupby(df['timestamp']).idxmin().sort_values()
                                                         # ^^^^^^^^^^^^^^^   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 2022-10-14
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多