Value Error Obtained while creating a new dataframe which contains the min and max of a particular column

Viewed 37

I have a data frame as shown below.I need to find the min and max of the column "Chip_Current[uAmp]" I used the below code to create a new data frame but obtained an error.

ValueError: 2 columns passed, passed data had 1 columns.

data = [[DEEP_SLEEP_PVT['Chip_Current[uAmp]'].min()], [DEEP_SLEEP_PVT['Chip_Current[uAmp]'].max()]]
df_DS = pd.DataFrame(data, columns = ['MIN', 'MAX'])

DEEP_SLEEP_PVT is my pivot table name

After running the code provided by Jerzel output is coming as expected. Please see below.

enter image description here

enter image description here

1 Answers

Use Series.aggregate, output is Series, so for one row DataFrame convert it to DataFrame and transpose:

df1 = DEEP_SLEEP_PVT['Chip_Current[uAmp]'].agg(['min','max']).to_frame().T

If need minimal and maximal per some another column/level like Device_ID use GroupBy.agg:

df2 = DEEP_SLEEP_PVT.groupby('Device_ID')['Chip_Current[uAmp]'].agg(['min','max'])

Or by multiple columns/levels:

df3 = (DEEP_SLEEP_PVT.groupby(['Device_ID', 'Temp(deg)'])['Chip_Current[uAmp]']
                     .agg(['min','max']))

Sample data:

DEEP_SLEEP_PVT = (pd.DataFrame({'Chip_Current[uAmp]':[5,0,-1,2,3,2,1,-5,7,4.8],
                               'Device_ID':['FF_2646'] * 5 + ['TT_2438'] * 5,
                               'Temp(deg)':[-20]*3 + [25] * 2 + [-20]*3 + [25] * 2})
                    .set_index(['Device_ID','Temp(deg)']))
    
print (DEEP_SLEEP_PVT)
                     Chip_Current[uAmp]
Device_ID Temp(deg)                    
FF_2646   -20                       5.0
          -20                       0.0
          -20                      -1.0
           25                       2.0
           25                       3.0
TT_2438   -20                       2.0
          -20                       1.0
          -20                      -5.0
           25                       7.0
           25                       4.8

df1 = DEEP_SLEEP_PVT['Chip_Current[uAmp]'].agg(['min','max']).to_frame().T
print (df1)
                    min  max
Chip_Current[uAmp] -5.0  7.0

df2 = DEEP_SLEEP_PVT.groupby('Device_ID')['Chip_Current[uAmp]'].agg(['min','max'])
print (df2)
           min  max
Device_ID          
FF_2646   -1.0  5.0
TT_2438   -5.0  7.0

df3 = DEEP_SLEEP_PVT.groupby(['Device_ID', 'Temp(deg)'])['Chip_Current[uAmp]'].agg(['min','max'])
print (df3)
                     min  max
Device_ID Temp(deg)          
FF_2646   -20       -1.0  5.0
           25        2.0  3.0
TT_2438   -20       -5.0  2.0
           25        4.8  7.0
Related