Problem on filtering data on csv using python

Question:

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:)
Asked By: ziyodullo jumayev

||

Answers:

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
Answered By: misterhuge
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.