将pandas.DataFrame索引保存到txt文件(Python)

时间:2017-08-17 12:26:21

标签: python pandas dataframe text indexing

我使用tsfresh从时间序列数据中提取特征,并将结果保存到pandas.DataFrame。如何使用以下命令仅提取调用的功能名称:

features.ix[0].index

在功能名称之前没有'u',并且能够将名称列保存为txt文件?

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)

2 个答案:

答案 0 :(得分:4)

'u'只是一个unicode前缀,不会写入文件,无论如何转换为系列并调用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'

答案 1 :(得分:1)

您可以使用np.savetxt

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

或者您可以使用to_csv功能

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

正如Ed所说,' u'只是一个unicode前缀,不会被写入文件。

注意 - 使用np.savetxt以附加模式写入,以附加模式打开文件并作为二进制文件,然后将处理程序传递给函数。

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