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

How to split csv files into multiple ones based on certain values of a dataframe (python)

I have a data as follows

ID A B
GIP1 .. ..
GIP1 .. ..
GIP2 .. ..
GIP2 .. ..
GIP3 .. ..

I want to split the csv file based on the ID. GIP1 feature csv files as GIP1.csv and GIP2 as GIP2.csv and so on.

Can anyone please help me how to do this in python?

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

>Solution :

So you can use the loc function in pandas. I am assuming you read your csv in as a pandas df

Now you can write a for loop like this:

df = pd.read_csv("gips.csv")
features = df.Id.unique()
for f in features:
    df_split = df.loc[df.ID == f]
    df_split.to_csv(f"{f}.csv")
 
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