larray.Array.to_csv

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

Write array to a csv file.

Parameters
filepathstr or Path

path where the csv file has to be written.

sepstr, optional

separator for the csv file. Defaults to ,.

na_repstr, optional

replace NA values with na_rep. Defaults to ‘’.

wideboolean, optional

Whether 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_namestr, 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 to write the last axis name (using ‘' ). Defaults to ‘default’.

dropnaNone, ‘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

>>> tmp_path = getfixture('tmp_path')
>>> fname = tmp_path / '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