Issue
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')
Solution
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']]
Answered By - Kevin Choon Liang Yew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.