Home > other >  Make a DataFrame with idxmax() outputs
Make a DataFrame with idxmax() outputs

Time:05-24

I am using idxmax to subset the maximum values of my dataframe and get their index values.

I put the output in a list, and convert it into a two-columns dataframe.

Unfortunately, one of the column is interpreted as the index. And using index=False or index=None does not work.

Would anyone know why?

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))

list=df.idxmax()

df = pd.DataFrame(list, index=False, columns = ['Y', 'Z'])

CodePudding user response:

Try this:

list.rename_axis('Y').reset_index(name='Z')

Output:

   Y   Z
0  A  42
1  B  88
2  C  47
3  D  82

Update:
list.rename_axis('Y').rename('Z').to_frame()

Output:

    Z
Y    
A  42
B  88
C  47
D  82
  • Related