user8188120
user8188120

Reputation: 915

Saving pandas.DataFrame index to txt file (Python)

I've used tsfresh to extract features from time series data and it saves the results to a pandas.DataFrame. How do I extract just the feature names called using the command:

features.ix[0].index

without the 'u' before the feature names and being able to save as a column of names as a txt file?

import numpy as np
import pandas as np

>>> (features.ix[0].index)
Index([ u'flux__time_reversal_asymmetry_statistic__lag_2',
                                          u'flux__median',
       u'flux__mean_abs_change_quantiles__qh_0.8__ql_0.0',
       u'flux__mean_abs_change_quantiles__qh_0.4__ql_0.8',
       u'flux__mean_abs_change_quantiles__qh_0.4__ql_0.4',
                  u'flux__sum_of_reoccurring_data_points',
       u'flux__mean_abs_change_quantiles__qh_0.4__ql_0.6',
       u'flux__mean_abs_change_quantiles__qh_0.4__ql_0.0',
                               u'flux__number_peaks__n_3',
       u'flux__mean_abs_change_quantiles__qh_0.4__ql_0.2',
       ...
                        u'flux__fft_coefficient__coeff_0',
                        u'flux__fft_coefficient__coeff_1',
                        u'flux__fft_coefficient__coeff_2',
                        u'flux__fft_coefficient__coeff_3',
                        u'flux__fft_coefficient__coeff_4',
                        u'flux__fft_coefficient__coeff_5',
                        u'flux__fft_coefficient__coeff_6',
                        u'flux__fft_coefficient__coeff_7',
                        u'flux__fft_coefficient__coeff_8',
                        u'flux__fft_coefficient__coeff_9'],
      dtype='object', length=222)

Upvotes: 1

Views: 3232

Answers (2)

BlueEagle
BlueEagle

Reputation: 106

You can use np.savetxt

np.savetxt(r'fileName.txt', features.ix[0].index.to_series())

or you can use the to_csv function

features.ix[0].index.to_series().to_csv(r'filename.txt', header=None, sep=' ', mode='a')

And as mentioned by Ed, the 'u' is just a unicode prefix and won't get written to file.

Note - To write in append mode using np.savetxt, open a file in append mode and as binary file and then pass the handler to the function.

fid = open('fileName.txt','ab')
series = np.random.rand(1,1)
np.savetxt(fid,series)
fid.close()

Upvotes: 1

EdChum
EdChum

Reputation: 394409

the 'u' is just a unicode prefix, this doesn't get written to file, anyway convert to a series and call to_csv:

features.ix[0].index.to_series().to_csv(your_file_path)

e.g.:

In[36]:
df.index.to_series().to_csv()
Out[36]: 'flux__time_reversal_asymmetry_statistic__lag_2,flux__time_reversal_asymmetry_statistic__lag_2\nflux__median,flux__median\nflux__mean_abs_change_quantiles__qh_0.8__ql_0.0,flux__mean_abs_change_quantiles__qh_0.8__ql_0.0\nflux__mean_abs_change_quantiles__qh_0.4__ql_0.8,flux__mean_abs_change_quantiles__qh_0.4__ql_0.8\nflux__mean_abs_change_quantiles__qh_0.4__ql_0.4,flux__mean_abs_change_quantiles__qh_0.4__ql_0.4\nflux__sum_of_reoccurring_data_points,flux__sum_of_reoccurring_data_points\nflux__mean_abs_change_quantiles__qh_0.4__ql_0.6,flux__mean_abs_change_quantiles__qh_0.4__ql_0.6\nflux__mean_abs_change_quantiles__qh_0.4__ql_0.0,flux__mean_abs_change_quantiles__qh_0.4__ql_0.0\nflux__number_peaks__n_3,flux__number_peaks__n_3\nflux__mean_abs_change_quantiles__qh_0.4__ql_0.2,flux__mean_abs_change_quantiles__qh_0.4__ql_0.2\nflux__fft_coefficient__coeff_0,flux__fft_coefficient__coeff_0\nflux__fft_coefficient__coeff_1,flux__fft_coefficient__coeff_1\nflux__fft_coefficient__coeff_2,flux__fft_coefficient__coeff_2\nflux__fft_coefficient__coeff_3,flux__fft_coefficient__coeff_3\nflux__fft_coefficient__coeff_4,flux__fft_coefficient__coeff_4\nflux__fft_coefficient__coeff_5,flux__fft_coefficient__coeff_5\nflux__fft_coefficient__coeff_6,flux__fft_coefficient__coeff_6\nflux__fft_coefficient__coeff_7,flux__fft_coefficient__coeff_7\nflux__fft_coefficient__coeff_8,flux__fft_coefficient__coeff_8\nflux__fft_coefficient__coeff_9,flux__fft_coefficient__coeff_9\n'

Upvotes: 3

Related Questions