1

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?

1 Answer 1

3

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")
 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.