larray.Array.to_csv

Array.to_csv(self, filepath, sep=', ', na_rep='', wide=True, value_name='value', dropna=None, dialect='default', **kwargs)[source]

Writes array to a csv file.

Parameters:
filepath : str

path where the csv file has to be written.

sep : str, optional

separator for the csv file. Defaults to ,.

na_rep : str, optional

replace NA values with na_rep. Defaults to ‘’.

wide : boolean, optional

Whether or not writing arrays in “wide” format. If True, arrays are exported with the last axis represented horizontally. If False, arrays are exported in “narrow” format: one column per axis plus one value column. Defaults to True.

value_name : str, optional

Name of the column containing the values (last column) in the csv file when wide=False (see above). Defaults to ‘value’.

dialect : ‘default’ | ‘classic’, optional

Whether or not to write the last axis name (using ‘’ ). Defaults to ‘default’.

dropna : None, ‘all’, ‘any’ or True, optional

Drop lines if ‘all’ its values are NA, if ‘any’ value is NA or do not drop any line (default). True is equivalent to ‘all’.

Examples

>>> tmpdir = getfixture('tmpdir')
>>> fname = os.path.join(tmpdir.strpath, 'test.csv')
>>> a = ndtest('nat=BE,FO;sex=M,F')
>>> a
nat\sex  M  F
     BE  0  1
     FO  2  3
>>> a.to_csv(fname)
>>> with open(fname) as f:
...     print(f.read().strip())
nat\sex,M,F
BE,0,1
FO,2,3
>>> a.to_csv(fname, sep=';', wide=False)
>>> with open(fname) as f:
...     print(f.read().strip())
nat;sex;value
BE;M;0
BE;F;1
FO;M;2
FO;F;3
>>> a.to_csv(fname, sep=';', wide=False, value_name='population')
>>> with open(fname) as f:
...     print(f.read().strip())
nat;sex;population
BE;M;0
BE;F;1
FO;M;2
FO;F;3
>>> a.to_csv(fname, dialect='classic')
>>> with open(fname) as f:
...     print(f.read().strip())
nat,M,F
BE,0,1
FO,2,3