【问题标题】:How to get Series list elements vertically in pandas如何在熊猫中垂直获取系列列表元素
【发布时间】:2017-02-16 12:00:57
【问题描述】:

我需要为 pandas 中的系列列表获取转置或列表示。下面是我用来从系列中形成列表的代码 sn-p-

 series1.index.values.tolist()
 series1.values.tolist()

它给出以下列表作为输出- ['A', 'B'....'Z'] , [4424180.0, 7463.0.....,34]

电流输出-

['A', 'B'....'Z'] , [4424180.0, 7463.0.....,34].

需要输出-

'A'   4424180
'B'   7463

【问题讨论】:

    标签: python-3.x pandas series


    【解决方案1】:

    你需要reset_index,可选rename_axis

    series1 = pd.Series([4424180.0, 7463.0,34], index=['A', 'B', 'Z'])
    print (series1)
    A    4424180.0
    B       7463.0
    Z         34.0
    dtype: float64
    
    df = series1.rename_axis('a').reset_index(name='b')
    print (df)
       a          b
    0  A  4424180.0
    1  B     7463.0
    2  Z       34.0
    

    df = series1.reset_index()
    df.columns = ['a','b']
    print (df)
       a          b
    0  A  4424180.0
    1  B     7463.0
    2  Z       34.0
    

    【讨论】:

    • 得到 AttributeError: 'list' object has no attribute 'reset_index' 为此。
    • series1pandas.Series 吗? print (type(series1))
    • 我的系列和你的真实数据有什么不同吗?
    • 得到了错误,在reset_index之前使用了values函数。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2023-03-25
    相关资源
    最近更新 更多