Python Pandas: pivot only certain columns in the DataFrame while keeping others

后端 未结 1 868
礼貌的吻别
礼貌的吻别 2020-12-15 22:10

I am trying to re-arrange a DataFrame that I automatically read in from a json using Pandas. I\'ve searched but have had no success.

I have the following json (saved

相关标签:
1条回答
  • 2020-12-15 22:57

    Your first attempt was nearly correct, just use columns='value_id' instead of including it in the index.

    # Perform the pivot.
    df = df.pivot_table(
        values='value',
        index=['stream_name', 'preferred_timestamp', 'internal_timestamp'],
        columns='value_id'
        )
    
    # Formatting.
    df.reset_index(inplace=True)
    df.columns.name = None
    

    This isn't an issue in your example data, but keep in mind that pivot_table will aggregate values if multiple values are pivoted to the same position (taking the mean by default).

    0 讨论(0)
提交回复
热议问题