How to get the total of combinations between two datasets and the sum of their attributes?

Asked

Viewed 62 times

1

I have two datasets (brake and gear). Both the brake date set and the Ambios data set have attributes (Force, Aero, Grip and Rely). I would like to get the total of combinations between these datasets, and the sum of their attributes. In the example above I put the two datasets with two pieces each and also an example of how I would like to view the result in the table "Expected result". This table shows me all the combinations between the 4 pieces and the sum of the attributes.

Dataset of brake:

|Level|Freio|Força|Aero|Grip|Rely|
|:----|:----|:----|:---|:---|:---|
|  8  |MINI |8    |13  |28  |5   |
|  10 |Fth  |8    |6   |28  |11  |

Data set of Cambio:

|Level|Cambio|Força|Aero|Grip|Rely|
|:----|:-----|:----|:---|:---|:---|
|  10 |Sd    |8    |6   |6   |16  |
|  8  |Smoke |15   |5   |5   |5   |

Expected result

Expected dataset

|Combinacao|Freio|Cambio|Força|Aero|Grip|Rely|
|:---------|:----|:-----|:----|:---|:---|:---|
|Mini Sd   |Mini |Sd    |16   |19  |34  |21  |
|Mini Smoke|Mini |Smoke |23   |18  |33  |10  |
|Fth Smoke |Fth  |Smoke |23   |11  |33  |16  |
|Fth Sd    |Fth  |Sd    |16   |12  |34  |27  |
  • You can describe better what you want to do?

  • Of course, help me understand if I was clear. I have two datasets (brake and gear). Both brakes and brakes have attributes (Force, Aero, Grip and Rely). I would like to get the total of combinations between these datasets, and the sum of their attributes. In the example above I put the two datasets with two pieces each and also an example of how I would like to view the result in the table "Expected result". This table shows me all the combinations between the 4 pieces and the sum of the attributes.

  • 1

    Great. Please click "edit" on the question and add this description.

  • 1

    Done, please let me know if it got better that way. Thank you

1 answer

2


The solution to this type of problem is DataFrame.combine (link to documentation), but how you want to combine columns with different names and make a "distributive" it takes a small workaround. Follows code:

import pandas as pd 

df1=pd.read_csv("data1.txt")
df2=pd.read_csv("data2.txt")

def my_sum(a,b):
    return a+b

dfp1=df1.set_index("Level").combine(df2.set_index("Level"), my_sum).drop(['Freio','Cambio'], axis=1)
lista_dfs=[df1.set_index("Level")[['Freio']],df2.set_index("Level")[['Cambio']]]
dfp1=dfp1.join(lista_dfs)
dfp1['Combinacao']=[dfp1['Freio'][k]+' '+dfp1['Cambio'][k] for k in dfp1.index]
dfp1=dfp1.reindex(['Combinacao', 'Freio', 'Cambio', 'Força','Aero','Grip','Rely'], axis=1)

df1.Level.replace({8:10,10:8},inplace=True)
dfp2=df2.set_index("Level").combine(df1.set_index("Level"), my_sum).drop(['Freio','Cambio'], axis=1)
lista_dfs=[df2.set_index("Level")[['Cambio']],df1.set_index("Level")[['Freio']]]
dfp2=dfp2.join(lista_dfs)
dfp2['Combinacao']=[dfp2['Freio'][k]+' '+dfp2['Cambio'][k] for k in dfp2.index]
dfp2=dfp2.reindex(['Combinacao', 'Freio', 'Cambio', 'Força','Aero','Grip','Rely'], axis=1)

df=dfp1.append(dfp2).reset_index(drop=True)
print(df)

Returns:

   Combinacao Freio Cambio  Força  Aero  Grip  Rely
0  MINI Smoke  MINI  Smoke     23    18    33    10
1      Fth Sd   Fth     Sd     16    12    34    27
2     MINI Sd  MINI     Sd     16    19    34    21
3   Fth Smoke   Fth  Smoke     23    11    33    16

If you intend to do this procedure often recommend debugging the code and creating a function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.