Home > Mobile >  Create a dataframe with operation result in Python
Create a dataframe with operation result in Python

Time:02-17

I have this two dataframes

    Name   Last_Name  Age
0      Alan      Lopez    18
1       Ana      Lopez    15
2      Juan      Lopez    13
3  Cristina  Hernandez    16
4      Lalo  Hernandez    15
5       Ana   Martinez    14
6   Alberto   Martinez    20
7   Alberto   Martinez    19

And a Second:

    Last_Name
0      Lopez
1  Hernandez
2   Martinez

And I want to create a third Data Frame where i can find the max value and rest the min value

    Last_Name Range 
0      Lopez 5
1  Hernandez 1
2   Martinez 6

What i tried is df3=np.where(df1['Last_Name'].eq(df2['Last_Name']), df1['Age'].max() - df1['Age'].min(),df2['Range'])but gives me an error about an unsupported operand for - 'method' and 'method'

CodePudding user response:

You can check with numpy ptp with groupby

out = df1.groupby('Last_Name')['Age'].agg(np.ptp).reindex(df2['Last_Name'])

CodePudding user response:

IIUC, you want groupby:

>>> df1.groupby("Last_Name")["Age"].max() - df1.groupby("Last_Name")["Age"].min()
Last_Name
Hernandez    1
Lopez        5
Martinez     6
Name: Age, dtype: int64
  • Related