Skip to content Skip to sidebar Skip to footer

How To Apply Filter Condition In Percentage String Column Using Pandas?

I am working on below df but unable to apply filter in percentage field,but it is working normal excel. I need to apply filter condition > 100.00% in the particular field using

Solution 1:

I am assuming that the values you have are read as strings in Pandas:

data = ['4,700.00%', '3,900.00%', '1,500.00%', '1,400.00%', '1,200.00%', '0.15%', '0.13%', '0.12%', '0.10%', '0.08%', '0.07%']

df = pd.DataFrame(data)
df.columns = ['data']

printing the df:

       data
0   4,700.00%
1   3,900.00%
2   1,500.00%
3   1,400.00%
4   1,200.00%
5      0.15%
6      0.13%
7      0.12%
8      0.10%
9      0.08%
10     0.07%

then:

df['data'] = df['data'].str.rstrip('%').str.replace(',','').astype('float')
df_filtered = df[df['data'] > 100]

Results:

     data
0  4700.0
1  3900.0
2  1500.0
3  1400.0
4  1200.0

Solution 2:

I have used below code as well.str.rstrip('%') and .str.replace(',','').astype('float') it working fine


Post a Comment for "How To Apply Filter Condition In Percentage String Column Using Pandas?"