简体   繁体   中英

Filtering the data based on values in the columns in pandas dataframe

I have been working on some data lately. During the filtering process, I found some columns have some issue. I want to only keep those rows which have ')' present at the last in the Branch columns.

I have tried several options but the i want to find fastest way to go around it.

这是我一直在研究的数据的一部分。

Since you did not provide your data as text, I have created an example dataframe:

Input:

d = {'college_name': ['College {}'.format(i+1) for i in range(8)], 'branch': ['Civil Enigineering '+ '(4 Years)'*(i%2) for i in range(8)]}
df = pd.DataFrame(data=d, columns=['college_name','branch'])
df

Output:

    college_name    branch
0   College 1   Civil Enigineering
1   College 2   Civil Enigineering (4 Years)
2   College 3   Civil Enigineering
3   College 4   Civil Enigineering (4 Years)
4   College 5   Civil Enigineering
5   College 6   Civil Enigineering (4 Years)
6   College 7   Civil Enigineering
7   College 8   Civil Enigineering (4 Years)

Pandas series have built in string processing methods. You can use str.endswith(')') to filter your data. Notice that df['branch'].str.endswith(')') will return a boolean mask.

Input:

df[df['branch'].str.endswith(')')]

Output:

    college_name    branch
1   College 2   Civil Enigineering (4 Years)
3   College 4   Civil Enigineering (4 Years)
5   College 6   Civil Enigineering (4 Years)
7   College 8   Civil Enigineering (4 Years)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM