Skip to main content
 首页 » 编程设计

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

2025年05月04日22EasonJim

我使用 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) 

请您参考如下方法:

'u' 只是一个 unicode 前缀,这不会写入文件,无论如何转换为系列并调用 to_csv :

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

例如:

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'