Sayed Sohan
Sayed Sohan

Reputation: 1499

Sort CSV file using given list pandas

I have a csv file which contains 2 column and a list which contains all the values of the first column randomly. I want to sort the csv file like the list contains the value.

CSV file:

 Name     age
 ----     ----
 alice    17
 bob      18
 carol    19

List

name = ['bob','carol','alice']

Expected ouput:

 Name     age
 ----     ----
 bob      18
 carol    19
 alice    17

How to do it with python pandas module??

Upvotes: 1

Views: 90

Answers (2)

Jano
Jano

Reputation: 455

You can turn the list into a pandas Series (assigning it the same name as the column you want to order) and then just merge. The merge should be inner, left or outer depending the overlap of the list and the file and how you want to deal with discrepancies

import pandas as pd

df_ages = pd.DataFrame([('alice', 17), ('bob', 18), ('carol', 19)], columns=['Name', 'age'])

name = ['bob','carol','alice']
name = pd.Series(name, name='Name')


df_ages = pd.merge(name, df_ages, how='outer')

result:

    Name  age
0    bob   18
1  carol   19
2  alice   17

Upvotes: 3

hardhypochondria
hardhypochondria

Reputation: 388

Here is python solution. You can use output file in padnas, if you really need to.

import csv

name = ['bob','carol','alice']

with open('in.csv') as f_in, open('out.csv', 'w') as f_out:
    rows = {n: a for n, a in csv.reader(f_in, delimiter=',')}
    writer = csv.writer(f_out)
    for n in name:
        writer.writerow([n, rows[n]])

Upvotes: 0

Related Questions