Home > Back-end >  Problem on filtering data on csv using python
Problem on filtering data on csv using python

Time:10-15

index,    name,     id,  status
1,        John,     500, online
2,        Anne,     485, offline
3,        Angel,    856, online
4,        Lusia,    777, offline

from this I want to get only names which have vowel endings. I expected this result:

index,    name,     id,  status
1,        Anne,     485, offline
2,        Lusia,    777, offline

that's why I have made a python code here is the exapmle:

so problems

  1. the numbers are not in the correct order.
  2. as you can see each rows has addition of " " symbol how can I fix this? please give me solutions:)

CodePudding user response:

try to avoid using apply(). Instead use a string method. (Assuming the 'numbers out of order' that you are referring to are the index numbers. To remedy that, use reset_index)

df = pd.read_csv('data.csv', skipinitialspace=True)
df = df.drop('index',axis=1)
df[df['name'].str.endswith(('a','e','i','o','u'))].reset_index(drop=True).to_csv('output.csv', index_label='index')

output.csv now has:

index,name,id,status
0,Anne,485,offline
1,Lusia,777,offline
  • Related