【问题标题】:Fill column in df.A based on comparison values in df.A and df.B根据 df.A 和 df.B 中的比较值填充 df.A 中的列
【发布时间】:2020-08-24 16:13:55
【问题描述】:

所以我有这个代码:

import pandas as pd
import numpy as np

frame1 = {'Season': ['S19', 'S20', 'S21',
                     'S19', 'S20', 'S21',
                     'S19', 'S20', 'S21'],
          'DateFrom': ['2019-01-01', '2020-01-01', '2021-01-01',
                       '2019-01-01', '2020-01-01', '2021-01-01',
                       '2019-01-01', '2020-01-01', '2021-01-01'],
          'DateTo': ['2019-12-30', '2020-12-30', '2021-12-30',
                     '2019-12-30', '2020-12-30', '2021-12-30',
                     '2019-12-30', '2020-12-30', '2021-12-30'],
          'Currency': ['EUR', 'EUR', 'EUR',
                       'USD', 'USD', 'USD',
                       'MAD', 'MAD', 'MAD'],
          'Rate': [1, 2, 3, 4, 5, 6, 7, 8, 9]
          }
df1 = pd.DataFrame(data=frame1)

frame2 = {'Room': ['Double', 'Single', 'SeaView'],
          'Season': ['S20', 'S20', 'S19'],
          'DateFrom': ['2020-05-01', '2020-07-05', '2019-03-25'],
          'Currency': ['EUR', 'MAD', 'USD'],
          'Rate': [0, 0, 0]
          }
df2 = pd.DataFrame(data=frame2)
df1[['DateFrom', 'DateTo']] = df1[['DateFrom', 'DateTo']].apply(pd.to_datetime)
df2[['DateFrom']] = df2[['DateFrom']].apply(pd.to_datetime)
print(df1.dtypes)
print(df2.dtypes)

df2['Rate'] = np.where((
                df2['Season'] == df1['Season'] &
                df2['Currency'] == df1['Currency'] &
                (df2['DateFrom'] > df1['DateFrom'] & df2['DateFrom'] < df1['DateTo'])
                        ), df1['Rates'], 'MissingData')

print(df2)

我想要实现的是根据以下条件使用 df1 中的 Rate 值填充 df2 中的 Rate 值:

df2.Season == df1.Season &

df2.Currency == df1.Currency &

df2.DateFrom must be between df1.DateFrom and df1.DateTo

所以我的“利率”结果应该是 2,8,4

我希望上面的代码能正常工作,但它没有,我收到错误:

"TypeError: &: 'str' and 'str' 的操作数类型不受支持"

任何帮助如何使它工作将不胜感激。

【问题讨论】:

    标签: pandas dataframe conditional-statements


    【解决方案1】:

    可以先合并再比较:

    out = df1.merge(df2[['Season','Currency','DateFrom']],on=['Season','Currency'],
                                                                suffixes=('','_y'))
    out = (out[out['DateFrom_y'].between(out['DateFrom'],out['DateTo'])]
          .reindex(columns=df1.columns).copy())
    

    print(out)
    
      Season   DateFrom     DateTo Currency  Rate
    0    S20 2020-01-01 2020-12-30      EUR     2
    1    S19 2019-01-01 2019-12-30      USD     4
    2    S20 2020-01-01 2020-12-30      MAD     8
    

    按 cmets 编辑:

    out = df1.merge(df2,on=['Season','Currency'],suffixes=('','_y'))
    out = (out[out['DateFrom_y'].between(out['DateFrom'],out['DateTo'])]
           .reindex(columns=df2.columns).copy())
    
          Room Season    DateFrom Currency  Rate
    0   Double    S20  2020-01-01      EUR     2
    1  SeaView    S19  2019-01-01      USD     4
    2   Single    S20  2020-01-01      MAD     8
    

    【讨论】:

    • 好的,这似乎可行,但如果我想将 df2 作为结果数据帧,而不是 df1,我需要更改什么?
    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 2022-12-11
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多