【问题标题】:how to insert a new integer index ipython pandas如何插入一个新的整数索引 python pandas
【发布时间】:2016-05-01 04:10:51
【问题描述】:

我从另一个数据帧创建了值计数数据帧 例如

                 freq
     0            2
     0.33333      10
     1.66667      13

自动地,它的索引是 0, 0.3333, 1.66667 并且索引可以是可变的 因为我打算根据特定值制作许多数据框

如何插入整数索引?

喜欢

                  freq
0   0              2
1   0.33333        10
2   1.66667        13

谢谢

【问题讨论】:

    标签: pandas indexing ipython


    【解决方案1】:

    你从values_count返回的结果是一个序列,要设置一个通用的1 ... n索引,你可以使用reset_index

    In [4]: s = pd.Series([0,0.3,0.3,1.6])
    
    In [5]: s.value_counts()
    Out[5]:
    0.3    2
    1.6    1
    0.0    1
    dtype: int64
    
    In [9]: s.value_counts().reset_index(name='freq')
    Out[9]:
       index  freq
    0    0.3     2
    1    1.6     1
    2    0.0     1
    

    【讨论】:

    • 为什么我得到 'reset_index() got an unexpected keyword argument 'name'' 消息??
    • 可能是因为您使用的是旧版本的 pandas。您可以忽略它,然后重命名列。
    猜你喜欢
    • 2012-11-13
    • 2015-10-19
    • 2015-06-12
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2017-05-22
    • 2019-05-04
    • 2021-07-28
    相关资源
    最近更新 更多