使用索引作为键和日期合并两个数据框

时间:2019-12-11 14:42:32

标签: python pandas dataframe

我正在尝试使用FK合并两个数据框,并在两个日期之间合并,然后将输出保存在新的数据框中。

考虑以下示例:

# first_df 
FK    date          value1   value2 ... (more columns)
1     2019-01-01    50       50
1     2019-01-02    40       80
1     2019-01-03    80       20
1     2019-01-04    18       44
1     2019-01-05    120      50
1     2019-01-06    80       0
1     2019-01-10    60       65
1     2019-01-15    25       44
1     2019-01-25    20       20
2     2019-01-01    50       40
2     2019-01-02    80       45
...............................


# second_df
FK    date          percentage
1     2019-01-01    50
1     2019-01-05    80
1     2019-01-10    40
1     2019-01-15    60
1     2019-01-25    90
2     2019-01-01    48
2     2019-01-08    40
2     2019-01-20    48
......................


# output_df
FK    date          value1            value2 ... (more columns)
1     2019-01-01    50% of 50 = 25    50% of 50 = 25
1     2019-01-02    50% of 40 = 20    50% of 80 = 40
1     2019-01-03    50% of 80 = 40    50% of 20 = 10
1     2019-01-04    50% of 18 = 9     50% of 44 = 22
1     2019-01-05    80% of 120 = 96   80% of 50 = 40
1     2019-01-06    80% of 80 = 64    80% of 0 = 0
1     2019-01-10    40% of 60 = 24    40% of 65 = 26
1     2019-01-15    60% of 25 = 15    60% of 44 = 26.4
1     2019-01-25    90% of 20 = 18    90% of 20 = 18
2     2019-01-01    48% of 50 = 24    48% of 40 = 19.2
2     2019-01-02    48% of 80 = 38.4  48% of 45 = 21.6

请注意,注意FK 2的第一条记录,索引是我的FK

该百分比应用于具有相同FK的所有记录,其中我的日期为: second_df.date <= first_df.date <和second_df.date_NEXT

例如,在2019-01-01和2019-01-04之间,我应用百分比50(来自second_df)

我一直在寻找一种干净且可读的实现方式...我知道我可以在fk上设置索引,并通过指定“ value1”列在我的df上使用apply。 但是,如果有超过5列的内容,您将如何处理?

希望您会了解我对大熊猫的了解很少


EDIT1

data1 = {'FK':[1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2],
             'date':['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', '2019-01-10', '2019-01-15', '2019-01-25', '2019-01-01', '2019-01-02'],
             'value1':[50, 40, 80, 18, 120, 80, 60, 25, 20, 50, 80]}
data2 = {'FK': [1, 1, 1, 1, 1, 2, 2],
             'date': ['2019-01-01', '2019-01-05', '2019-01-10', '2019-01-15', '2019-01-25', '2019-01-01',
                      '2019-01-08'],
             'percentage': [50, 80, 40, 60, 90, 48, 40]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
merged_df = pd.merge_asof(df1.sort_values('date'), df2.sort_values('date'), by='FK', on='date').sort_values('FK')

使用上述数据集发生了意外:Function call with ambiguous argument types

如果您有此异常,这是因为您的列“ on”(在我的情况下为FK)不被视为日期,而是字符串。

根据熊猫文档:[...]Furthermore this must be a numeric column, such as datetimelike, integer, or float.

1 个答案:

答案 0 :(得分:3)

在您的情况下,我们使用from functools import wraps from django.http import HttpResponseBadRequest, JsonResponse def query_params(*param_names): def decorator(func): @wraps(func) def inner(request, *args, **kwargs): try: params = {name: request.GET[name] for name in param_names} except KeyError: return HttpResponseBadRequest("Missing Parameter") kwargs.update(params) return func(request, *args, **kwargs) return inner return decorator @query_params("part") def get_part_info(request, part): # do something with part return JsonResponse({"result": part})

merge_asof

然后我们在同一df中有df=pd.merge_asof(df1.sort_values('date'),df2.sort_values('date'),by='FK',on='date').sort_values('FK') 和值,我们可以做多个

percentage