Home > Software design >  Explode column of arrays when saving dataframe to csv without dots
Explode column of arrays when saving dataframe to csv without dots

Time:02-24

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?

CodePudding user response:

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])
  • Related