我正在努力
pivot
双范畴变量的(
status_type
)在
python
pandas
但它导致
multiindex
数据帧。我希望这是一个正常的数据帧,但不知道怎么做。如果有任何帮助,我将不胜感激。
import pandas as pd
import numpy as np
# data
test_df = pd.DataFrame({"id": np.arange(0, 8),
"Window": np.random.rand(8),
"status_type" : ["snap","perf","snap","perf","snap","perf","snap","perf"],
"status_level": [1, 2, 20, 35, 10, 5, 42, 9],
})
test_df
数据透视:
test_df.pivot(index=['id','Window'],columns='status_type',values='status_level')
结果为多索引df:
我已经尝试了以下代码将其重置为正常的数据帧并删除
状态_类型
专栏,但没用。
(test_df
.pivot(index=['id','Window'], columns='status_type', values='status_level')
.reset_index()
.drop('status_type', axis = 1)
)