Pandas: How to set index to a column value, group columns, and return values

Gusta

I have a df as follows:

         State  Country   Pop       GDP
   1     MD     USA        433100   1222
   2     TX     USA        99934123 324234
   3     B.C.   MEX        324599   5343
   4     OAX    MEX        43322    986
   5     BC     CAN        3431     643
   6     NS     CAN        8749     3535

I want to group by Country and find the max GDP and min GDP, returning that as well as new column names so that it looks something like this:

      PoorState   GDP    Pop       RichState   GDP      Pop
USA   MD          1222   433100    TX          324234   99934123
MEX   OAX         986    43322     B.C.        5343     324599
... 

I've tried this to get the min and max for each state:

df.groupby('state').agg({'GDP':min, 'GDP':max})[['GDP','GDP']].reset_index()

but just the min works while the max doesn't.

  State   GDP    GDP
  MD      1222   1222
  OAX     986    986

-- I know that I can do this to set the index: newdf = df.set_index('region')

I think I'm trying to figure out if I can do this all on one line.

BENY

We can do groupby with rank create the category then pivot

df['new'] =  df.groupby('Country')['GDP'].rank().map({1:'Poor',2:'Rich'})
out = df.pivot(index='Country',columns = 'new').sort_index(level=1,axis=1)
out.columns = out.columns.map('_'.join)
out.reset_index(inplace=True)
out
Out[348]: 
  Country  GDP_Poor  Pop_Poor State_Poor  GDP_Rich  Pop_Rich State_Rich
0     CAN       643      3431         BC      3535      8749         NS
1     MEX       986     43322        OAX      5343    324599       B.C.
2     USA      1222    433100         MD    324234  99934123         TX

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas dataframe: how to group by values in a column and create new columns out of grouped values

Perform value counts in Python/Pandas on one column, but return values in multiple columns

How to groupby by a column and return the values of other columns as lists in pandas?

Pandas group by 2 columns, apply function, select max value and return index values

Group by column value and set it as index in Pandas

How to return the index (or column) lable of a value if column (or index) is known in pandas

How to return max value and associated index in Pandas dataframe column

Pandas Dataframe - Group by column value and lookup values from other columns

how to return columns values with the input of other column values of same row using pandas?

Return column name from index and value with pandas

How do I group by a column, and count values in separate columns (Pandas)

Pandas: How to group by one column and show count for unique values for all other columns per group?

Pandas: set preceding values conditional on current value in column (by group)

Pandas - set index values as column names

Pandas return separate column value in current index if two separate columns match

How do I return the index of a group of values from a list for a specific value in the group?

pandas set columns based on values of the same column

How to return value from "other" row using index column in Pandas

Is there a way to uniquely group by a set of column values in Pandas?

How to copy all column values of a dataframe into new columns of another one according to the index of the first and a column value of the second

Group various columns in pandas with list of column values

Pandas How to group columns by their values

Pandas find the first occurrence of a specific value in a row within multiple columns and return column index

Set values in a column based on the values of other columns as a group

How to group by column values into index?

How to unstack unique column values to columns and set another column as row index in Python Pandas

How to set group by column-name as column instead of index?(Pandas)

Pandas Set Index Based On Column Value

How to return values of two date ranges, from the same date column and value column, in two different columns as a result?