假设我有以下数据框“ 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
我需要将其转换为以下数据框“ 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
因此,第一个值将被归类为类型列,其中包含合计或可计费值。
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)
答案 0 :(得分: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)
收益
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
然后result.set_index('service')
将使service
成为索引,
但我建议您避免这样做,因为service
的值不是唯一的。
答案 1 :(得分:2)
看起来像df.stack()
的工作,其中有多个DataFrame.rename()
df.rename(index=str, columns={"utilization": "total", "utilization_billable": "billable"})\
.stack().reset_index(1).rename(index=str, columns={"level_1": "type", 0: "utilization"})\
.sort_values(by='type', ascending = False)
输出:
type utilization
service
1 total 10.0
2 total 30.0
3 total 40.0
4 total 40.0
1 billable 5.0
2 billable 20.0
3 billable 30.0
4 billable 32.0
答案 2 :(得分:2)
在第一列中添加后缀后,可以使用pd.wide_to_long
完成此操作。
import pandas as pd
df = df.rename(columns={'utilization': 'utilization_total'})
pd.wide_to_long(df.reset_index(), stubnames='utilization', sep='_',
i='service', j='type', suffix='.*').reset_index(1)
type utilization
service
1 total 10.0
2 total 30.0
3 total 40.0
4 total 40.0
1 billable 5.0
2 billable 20.0
3 billable 30.0
4 billable 32.0