Python根据pandas中的时间戳查找多个值

问题描述 投票:1回答:1

我有一个如下所示的数据框:

d = {'from': ['apple', 'banana', 'orange', 'banana', 'apple', 'orange'],
     'to': ['banana', 'orange', 'apple', 'orange', 'banana', 'apple'],
     'month': ['Aug-18', 'Aug-18', 'Aug-18', 'Sep-18', 'Sep-18','Sep-18']}

df = pd.DataFrame(data=d)

出:

     from      to    month
0   apple  banana   Aug-18
1  banana  orange   Aug-18
2  orange   apple   Aug-18
3  banana  orange   Sep-18
4   apple  banana   Sep-18
5  orange   apple   Sep-18

我有一个CSV参考表/查找表,如下所示:

product  start_date  end_date   weight grade
apple    01/06/2018  31/08/2018 heavy   a
orange   01/06/2018  31/08/2018 heavy   c
banana   01/06/2018  31/08/2021 heavy   b
apple    01/09/2018  31/12/2021 small   a
orange   01/09/2018  31/12/2021 heavy   a

注意:在参考/查找中,维度可能会逐月更改。

我需要在我的数据框中插入4个新列,名为:(1)from_weight,(2)to_weight,(3)from_grade(4)to_grade。并根据时间戳将数据框中的值与引用表合并以获得此结果:

     from      to    month     from_weight to_weight from_grade to_grade
 0   apple  banana   Aug-18       heavy     heavy          a        b
 1  banana  orange   Aug-18       heavy     heavy          b        a
 2  orange   apple   Aug-18       heavy     heavy          a        a
 3  banana  orange   Sep-18       heavy     heavy          b        a
 4   apple  banana   Sep-18       small     heavy          a        b
 5  orange   apple   Sep-18       heavy     small          a        a
python pandas merge timestamp
1个回答
0
投票

希望这涵盖所有情况,但仅通过提供的示例无法确定。我想“CSV引用”总是在月的第一天/最后一天开始/结束(否则你必须告诉我们如何处理这些情况)。

grade.csv

product,start_date,end_date,weight,grade
apple,01/06/2018,31/08/2018,heavy,a
orange,01/06/2018,31/08/2021,heavy,c
banana,01/06/2018,31/08/2021,heavy,b
apple,01/09/2018,01/01/2021,small,a
orange,01/06/2018,31/08/2021,heavy,a

解:

import pandas as pd
from dateutil import parser
import datetime as dt

d = {'from': ['apple', 'banana', 'orange', 'banana', 'apple', 'orange'],
     'to': ['banana', 'orange', 'apple', 'orange', 'banana', 'apple'],
     'month': ['Aug-18', 'Aug-18', 'Aug-18', 'Sept-18', 'Sept-18','Sept-18']}

df = pd.DataFrame(data=d, columns=list(d.keys()) + ['from_weight', 'to_weight', 'from_grade', 'to_grade'])

grade = pd.read_csv('grade.csv')

for entry in df.index:
     date = parser.parse(df.loc[entry, 'month'])
     for line in grade.index:
          date_start = dt.datetime.strptime(grade.loc[line, 'start_date'], '%d/%m/%Y')
          date_end = dt.datetime.strptime(grade.loc[line, 'end_date'], '%d/%m/%Y')
          if (df.loc[entry, 'from'] == grade.loc[line, 'product']) & (date > date_start) & (date < date_end):
               df.loc[entry, 'from_weight'] = grade.loc[line, 'weight']
               df.loc[entry, 'from_grade'] = grade.loc[line, 'grade']

          if (df.loc[entry, 'to'] == grade.loc[line, 'product']) & (date > date_start) & (date < date_end):
               df.loc[entry, 'to_weight'] = grade.loc[line, 'weight']
               df.loc[entry, 'to_grade'] = grade.loc[line, 'grade']


print(df)

输出:

from      to    month from_weight to_weight from_grade to_grade
0   apple  banana   Aug-18       heavy     heavy          a        b
1  banana  orange   Aug-18       heavy     heavy          b        a
2  orange   apple   Aug-18       heavy     heavy          a        a
3  banana  orange  Sept-18       heavy     heavy          b        a
4   apple  banana  Sept-18       small     heavy          a        b
5  orange   apple  Sept-18       heavy     small          a        a
© www.soinside.com 2019 - 2024. All rights reserved.