Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a .csv file that has data like that:

index, name,    id
1      john     512
2      Anne     895
3      Angel    897
4      Lusia    777
So I want to filter them by name endings and get names only which have vowel endings. And the result must be like that:

    index, name,     id
    1      Anne     895
    2      Lusia    777
After filtering, I want to save the result in another .csv file. I am trying various ways to get the correct result, However, I could not do that. please help me :(


What I have tried:

pandas.read_csv and pandas.DataFrame.to_csv to read and write csv files. For selection you can use something like df[df.name.str[-1].apply(lambda x: x in ['a', 'e', 'i', 'o', 'u'])]
Posted
Updated 13-Oct-22 19:19pm

1 solution

Did you try something like below.

Filtering:
Python
df[df.name.str[-1].apply(lambda x: x in ['a', 'e', 'i', 'o', 'u'])]
Refer: Lambda and filter in Python Examples[^]

Saving to CSV:
Python
df.to_csv('new-location\\output_filtered_sample1.csv', index=False, quoting=1)
Refer: pandas.DataFrame.to_csv — pandas 1.5.0 documentation[^]

Overall:
Python
df = pandas.read_csv('location-of-file\\sample1.csv')
filtereddf = df[df.name.str[-1].apply(lambda x: x in ['a', 'e', 'i', 'o', 'u'])]
filtereddf.to_csv('new-location\\output_filtered_sample1.csv', index=False, quoting=1)

try out!
 
Share this answer
 
Comments
MR-XAN777 14-Oct-22 13:59pm    
yeah bro this worked but it has some problems:
1. the result is coming with symbols.
original: 4169,Noiba,1030842495,2022-10-11 15:00:43+00:00
output: "4169","Noiba","1030842495","2022-10-11 15:00:43+00:00"

2. I want to get results by order
example:
I want:
1
2
3
The result is:
1
9
8
it is the original row of the first CSV files.
how we can solve this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900