Merge two DataFrames based on columns and values of a specific column with Pandas in Python 3.x

2024/9/21 16:41:13

Hello i have a problem which i am not able to implement a solution on. I have following two DataFrames:

>>> df1
A  B   date
1  1  01-2016
2  1  02-2017
1  2  03-2017
2  2  04-2020>>> df2
A  B  01-2016  02-2017  03-2017  04.2020
1  1    0.10    0.22     0.55     0.77
2  1    0.20    0.12     0.99     0.125
1  2    0.13    0.15     0.15     0.245
2  2    0.33    0.1      0.888    0.64

What i want is following DataFrame:

>>> df3
A  B   date      value
1  1  01-2016    0.10
2  1  02-2017    0.12
1  2  03-2017    0.15
2  2  04-2020    0.64

I already tried following:

        summarize_dates = self.summarize_specific_column(data=df1, column='date')for date in summarize_dates:left_on = np.append(left_on, date)right_on = np.append(right_on, merge_columns.upper())result = pd.merge(left=df2, right=df1,left_on=left_on, right_on=right_on,how='right')print(result)

This does not work. Can you help me and suggest a more comfortable implementation? Manyy thanks in advance!

Answer

You can melt df2 and then merge using the default 'inner' merge

df3 = df1.merge(df2.melt(id_vars = ['A', 'B'], var_name='date'))A   B   date    value
0   1   1   01-2016 0.10
1   2   1   02-2017 0.12
2   1   2   03-2017 0.15
3   2   2   04-2020 0.64
https://en.xdnf.cn/q/72038.html

Related Q&A

Use range as a key value in a dictionary, most efficient way?

I have been wondering if there is some kind of data-structure or clever way to use a dictionary (O(1) lookup) to return a value if there are given values for defined ranges that do not overlap. So far …

How to replace all instances of a sub-sequence in a list in Python?

I currently use this code:""" Replace all occurrences of subsequence a with b in list l """ def replace_subsequence(l,a,b):for i in range(len(l)):if(l[i:i+len(a)] == a):l…

How to initialise a 2D array in Python?

Ive been given the pseudo-code:for i= 1 to 3for j = 1 to 3board [i] [j] = 0next jnext iHow would I create this in python?(The idea is to create a 3 by 3 array with all of the elements set to 0 using a…

numpy: broadcast multiplication over one common axis of two 2d arrays

Im looking for a way to element-wise multiply two 2d arrays of shape (a, b) and (b, c), respectively. Over the b axis, which the two arrays have in common.For instance, an example of what Id like to br…

Convert integer to binary in python and compare the bits

How to convert a int n into binary and test each bit of the resulting binary number?I have just got the following after a lot of googling:def check_bit_positions(n, p1, p2):print int(str(n),2)However …

python, confused in decorate and closure

I have some test code:def num(num):def deco(func):def wrap(*args, **kwargs):inputed_num = numreturn func(*args, **kwargs)return wrapreturn deco@num(5) def test(a):return a + inputed_numprint test(1)whe…

Python Regex - checking for a capital letter with a lowercase after

I am trying to check for a capital letter that has a lowercase letter coming directly after it. The trick is that there is going to be a bunch of garbage capital letters and number coming directly befo…

Read hierarchical (tree-like) XML into a pandas dataframe, preserving hierarchy

I have a XML document that contains a hierarchical, tree-like structure, see the example below.The document contains several <Message> tags (I only copied one of them for convenience).Each <Me…

Reference counting using PyDict_SetItemString

Im wondering how memory management/reference counting works when a new value is set into an existing field within a PyDict (within a C extension).For instance, assume a dictionary is created and popula…

How to use statsmodels.tsa.seasonal.seasonal_decompose with a pandas dataframe

from statsmodels.tsa.seasonal import seasonal_decomposedef seasonal_decomp(df, model="additive"):seasonal_df = Noneseasonal_df = seasonal_decompose(df, model=additive)return seasonal_dfseason…