【问题标题】:Print pandas index in a pretty way以漂亮的方式打印 pandas 索引
【发布时间】:2017-08-10 10:11:42
【问题描述】:
print('Selected Features are {}'.format([X.columns[l1_rfe.get_support()]]))

给予

Selected Features are [Index([u'number_of_reviews', u'Occupations_art_culture_CSD',
   u'Occupations_natural_resources_CSD', u'Average_income_2005_CSD',
   u'Airport_id', u'Visitor_centre_id', u'City_Pop_2000',
   u'First_responder_id_ambulance', u'First_responder_id_fire',
   u'First_responder_lat_fire', u'events_10km', u'casinos_10km',
   u'transportation_10km', u'food_drinks_10km', u'boat_tours_10km'],
  dtype='object')]

有什么方法可以让这个结果更漂亮吗? get_support 来自 sklearn.feature_selection.RFECV,如果这对您很重要。

【问题讨论】:

    标签: python pandas scikit-learn


    【解决方案1】:

    这是使用get_values()的一种方法

    为了让事情可重现,我们来看看这个df:

    df1 = pd.DataFrame(np.random.randn(6,4),
                         index=list('abcdef'),
                         columns=list('ABCD'))
    
             A         B         C         D
    a -0.168137  0.064138 -0.278186  0.069164
    b  1.336853 -1.366735 -0.932872 -0.068140
    c  0.986338 -0.253487 -2.069040 -0.017315
    d -0.249902  0.597127 -0.079737 -1.128560
    e -0.422776  1.497260 -0.774697 -0.593360
    f  1.161005 -1.174774 -0.719904  0.634449
    

    印刷:

    In [20]: print('Features are: {}'.format([i for i in df1.index.get_values()]))
    Features are: ['a', 'b', 'c', 'd', 'e', 'f']
    

    【讨论】:

      【解决方案2】:

      这完全取决于你所说的更漂亮的意思,但如果是我,我会将此索引转换为列表,然后转换为字符串(立即转换为字符串会使“索引元数据”被抑制,删除括号、unicode 标记(使用正则表达式)和引号,然后以我喜欢的方式对其进行格式化(可能在每个元素后添加 \n)。您可能也想使用 pprint。

      【讨论】:

        猜你喜欢
        • 2013-05-27
        • 2011-03-21
        • 2017-05-26
        • 2016-08-16
        • 2014-07-06
        • 2011-05-06
        • 1970-01-01
        • 2015-02-07
        相关资源
        最近更新 更多