2

I have a dataframe that is 100,227 records long.

I would like to export this dataframe into 3 equally sized csv's

I did the following but getting an error. Is there perhaps a simpler approach to doing this?

df_seen = pd.read_csv("data.csv")

df1 = df_seen.shape.iloc[:, :33409]
df2 = df_seen.shape.iloc[33410:, 66818:]
df3 = df_seen.shape.iloc[66819:, 100227]

df1.to_csv('data1.csv', index=False)
df2.to_csv('data2.csv',index = False)
df3.to_csv('data3.csv',index = False)

So for example, the data looks similar to below where I would take the 1 column of 15 numbers and split it into 3 columns of 5 each:

Numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Desired out put (note each column represents a new csv with a single column)

1  6  11
2  7  12
3  8  13 
4  9  14
5  10 15
Blackdynomite
  • 421
  • 1
  • 4
  • 18
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. "Getting an error" is not a problem specification. – Prune Aug 04 '21 at 18:55
  • Your posted code does not run. Please [include a minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of your MRE. – Prune Aug 04 '21 at 18:55

1 Answers1

4

Try using numpy.array_split:

import numpy as np
df1, df2, df3 = np.array_split(df_seen, 3)

To save each DataFrame to a separate file, you could do:

for i, df in enumerate(np.array_split(df_seen, 3)):
    df.to_csv(f"data{i+1}.csv", index=False)
not_speshal
  • 22,093
  • 2
  • 15
  • 30