Home > front end >  Pandas data frame select all rows less than a column content float values
Pandas data frame select all rows less than a column content float values

Time:07-15

I have a data frame as shown below.I need to select all rows from the data frame where column values are less than or equal to 0.3768. But I am getting an error.

Below is my dataframe.

enter image description here

I need to select all rows where column value is less than or equal to 0.3768.

Below is my code

for column in df_thd_funct_mode1_T:
 
new_df = df_thd_funct_mode1_T.loc[(np.isclose(df_thd_funct_mode1_T[column] <= .3768))]

I am getting an error as shown below.

TypeError: '<=' not supported between instances of 'str' and 'float'

May I know how to solve this issue

CodePudding user response:

You could try this here:

new_df = df_thd_funct_mode1_T[df_thd_funct_mode1_T.apply(lambda row: any(float(column) <= .3768 for column in row), axis=1)]

Here you do not need the loop any more that you had in your example.

I basically go through the dataframe there and for each row I check, whether there is any value, that is less than .3768.

This of course will only work under the condition, that all columns only contain floats. If not, then you will run into an Error, trying to cast that into a float.

CodePudding user response:

If you want to check multiple columns you have to add to condition every column check. Every condition should be like:

df.loc[df['column_name'] <= 0.3768 ]

Check these examples it should be helpful to your question: https://www.statology.org/pandas-select-rows-based-on-column-values/

  • Related