Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read in a specific way from a csv file with pandas python

I have a data in a csv file here is a sample:

firstnb,secondnb,distance
901,19011,459.73618164837535
901,19017,492.5540450352788
901,19018,458.489289271722
903,13019,167.46632044684435
903,13020,353.16001204909657

the desired output:

901,19011,19017,19018
903,13019,13020

As you can see in the output I want to take firstnb column (901/903)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and put beside each one the secondnb I believe you can understand from the desired output better than my explanation 😀

What I tried so far is the following:

import pandas as pd
import csv
df = pd.read_csv('test.csv')
    with open('neighborList.csv','w',newline='') as file:
        writer = csv.writer(file)
        secondStation = []
        for row in range(len(df)):
            firstStation = df['firstnb'][row]
            for x in range(len(df)):
                if firstStation == df['firstnb'][x]:
                    secondStation.append(df['secondnb'][x])
                    # line = firstStation ,secondStation
                    # writer.writerow(line)        
            print(firstStation,secondStation)
            secondStation = []

my code output this :

901 [19011, 19017, 19018]
901 [19011, 19017, 19018]
901 [19011, 19017, 19018]
903 [13019, 13020]
903 [13019, 13020]

>Solution :

Pandas has a built in function to do this, called groupby:

df = pd.read_csv(YOUR_CSV_FILE) 
df_grouped = list(df.groupby(df['firstnb'])) # group by first column

# chain keys and values into merged list
for key, values in df_grouped:
    print([key] + values['secondnb'].tolist())

Here I just print the sublists; you can save them into a new csv in any format you’d like (strings, ints, etc)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading