pythonlearner
pythonlearner

Reputation: 91

how to apply filter condition in percentage string column using pandas?

original dataI 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 pandas.

I tried reading it from Html,csv and excel in pandas but unable to use condition. it requires float conversion but not working with given data

Upvotes: 0

Views: 1763

Answers (2)

Tabbakhh
Tabbakhh

Reputation: 460

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

Upvotes: 3

pythonlearner
pythonlearner

Reputation: 91

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

Upvotes: 0

Related Questions