1

Hello i have dataset containing 4 columns

x       y     z      s
1      42.8  157.5   1
1      43.8  13.5    1
1      44.8  152     2
         .
         .
         .
4      7528  157.5   2
4      45.8  13.5    3
8      72.8  152     3

i want to split my dataframe into separate csv files by their "s" column but I couldn't figure out a proper way of doing it.

"s" column has arbitrary numbers of labels. We don't know how many 1's or 2's dataset has. it is until 30 but not every number is contained in this dataset.

My desired output is:

df1
x       y     z      s
1      42.8  157.5   1
           .
1      43.8  13.5    1

df2
1      44.8  152     2
           .
4      7528  157.5   2

df3
4      45.8  13.5    3
           .
8      72.8  152     3

after I get this split I can easily write it to separate csv files. The problem I am having is that I don't know how many different "s" values I have and how much from each of them.

Thank you

Nightingale
  • 133
  • 1
  • 7

1 Answers1

2

Just groupby before sending to csv to do this dynamically:

for i, x in df.groupby('s'): x.to_csv(f'df{i}.csv', index=False)
David Erickson
  • 16,433
  • 2
  • 19
  • 35