pandas数据框:将2列(值,值)转换为2列(值,类型)

Dmi*_*nov 2 python pandas

假设我有以下数据框“ A”

         utilization  utilization_billable
service                                   
1               10.0                   5.0
2               30.0                  20.0
3               40.0                  30.0
4               40.0                  32.0
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为以下数据框“ B”

         utilization      type
service                       
1               10.0     total
2               30.0     total
3               40.0     total
4               40.0     total
1                5.0  billable
2               20.0  billable
3               30.0  billable
4               32.0  billable
Run Code Online (Sandbox Code Playgroud)

因此,来自第一个的值将被归类为类型列,其值是total或billable。

data = {
    'utilization': [10.0, 30.0, 40.0, 40.0],
    'utilization_billable': [5.0, 20.0, 30.0, 32.0],
    'service': [1, 2, 3, 4]
}
df = pd.DataFrame.from_dict(data).set_index('service')
print(df)

data = {
    'utilization': [10.0, 30.0, 40.0, 40.0, 5.0, 20.0, 30.0, 32.0],
    'service': [1, 2, 3, 4, 1, 2, 3, 4],
    'type': [
        'total',
        'total',
        'total',
        'total',
        'billable',
        'billable',
        'billable',
        'billable',
    ]
}
df = pd.DataFrame.from_dict(data).set_index('service')
print(df)
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以转换数据帧并执行这种分类?

unu*_*tbu 5

您可以使用pd.melt

import pandas as pd
data = {
    'utilization': [10.0, 30.0, 40.0, 40.0],
    'utilization_billable': [5.0, 20.0, 30.0, 32.0],
    'service': [1, 2, 3, 4]}

df = pd.DataFrame(data)
result =  pd.melt(df, var_name='type', value_name='utilization', id_vars='service')
print(result)
Run Code Online (Sandbox Code Playgroud)

产量

   service                  type  utilization
0        1           utilization         10.0
1        2           utilization         30.0
2        3           utilization         40.0
3        4           utilization         40.0
4        1  utilization_billable          5.0
5        2  utilization_billable         20.0
6        3  utilization_billable         30.0
7        4  utilization_billable         32.0
Run Code Online (Sandbox Code Playgroud)

这样result.set_index('service')就可以service建立索引,但是我建议避免这样做,因为service值不是唯一的。