2

I have a dataframe which contains a column 'sample' with arrays:enter image description here

When saving this a csv using to_csv, it prints literal '...' as follows:

enter image description here

How do I save the dataframe to a CSV where the arrays are expanded to a simple CSV, printing the entire contents of arrays without the '...'s?

2
  • Does this answer your question? stackoverflow.com/questions/53316471/… Commented Feb 22, 2022 at 21:19
  • Close, but it breaks the array with newlines when printing to csv, so it's still not in a usable state. I need it to be saved such that it can be re-read as a CSV by Pandas with no changes. Commented Feb 22, 2022 at 21:50

1 Answer 1

2

Converting the array to string and replacing the newline chars is working for me.

import numpy as np
import pandas as pd

df = pd.DataFrame({'sample':[np.array(range(99999, 99999 + 1000))]})

df['sample'] = df['sample'].apply(lambda x: str(x).replace('\n', ''))

df.to_csv('sample.csv', index=False)

If you want to re-read the .csv and get your array back, you can use literal_eval. The first replace is to get rid of the extra space preceding the array values.

from ast import literal_eval
new_df = pd.read_csv('sample.csv')
new_df['array_col'] = new_df['sample'].apply(lambda x: np.array(literal_eval(x.replace('[ ', '[').replace(' ', ','))))



print(new_df.loc[0, 'array_col'][0:10])

Output:

array([ 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006,
       100007, 100008])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.