相关疑难解决方法(0)

Pandas:如何将某些列移动到行中?

假设我有df以下内容。我想合并价格列和价值列,以便所有价格都在一个列中,所有数量都在另一列中。我还想要第三列来确定价格水平。例如,unit1unit2unit3

import numpy as np
import pandas as pd
df = pd.DataFrame(
    {
        'uid': ['U100', 'U200', 'E100', 'E200', 'E300', 'A100', 'A200', 'A300', 'A400', 'A500'],
        'location': ['US', 'US', 'EU', 'EU', 'EU', 'Asia', 'Asia', 'Asia', 'Asia', 'Asia'],
        'unit1_price': [10, 20, 15, 10, 10, 10, 20, 20, 25, 25],
        'unit1_vol': [100, 150, 100, 200, 150, 150, 100, 200, 200, 200],
        'unit2_price': [10, 25, 30, 20, 10, 10, 10, 10, 20, 20],
        'unit2_vol': [200, 200, 150, …
Run Code Online (Sandbox Code Playgroud)

python unpivot dataframe pandas

11
推荐指数
2
解决办法
2608
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1

unpivot ×1