Home > Software design >  Data frame in pandas, new df with columns and condition. Code into a one-line
Data frame in pandas, new df with columns and condition. Code into a one-line

Time:10-15

I have this dataframe df. I create a new dataframe with 'alcohol', 'malic_acid', 'ash', 'color_intensity', 'hue' as columns and the 'proline' > 1000. I have successfully solved this but with two lines (excluding the call for sub_df).

df2 = df[df['proline'] > 1000]
sub_df = df2[['alcohol', 'malic_acid', 'ash', 'color_intensity', 'hue']]
sub_df

I tried to make it a one-liner as

sub_df = (df[['alcohol', 'malic_acid', 'ash', 'color_intensity', 'hue']]) & (df[df['proline'] > 1000])
sub_df

But I get the error

TypeError: unsupported operand type(s) for &: 'float' and 'float'

I also tried

sub_df =  df[(df['alcohol', 'malic_acid', 'ash', 'color_intensity', 'hue']) & (df[df['proline'] > 1000])]
sub_df

but got the error

KeyError: ('alcohol', 'malic_acid', 'ash', 'color_intensity', 'hue')

CodePudding user response:

Your two codes can be put together, but just a matter of which to put first.

df2 = df[df['proline'] > 1000][['alcohol', 'malic_acid', 'ash', 'color_intensity', 'hue']]

Or better, use df.loc

df2 = df.loc[df['proline'] > 1000, ['alcohol', 'malic_acid', 'ash', 'color_intensity', 'hue']]
  • Related