作者:坨大大 | 来源:互联网 | 2023-09-08 17:43
获取数据框的代码 df
import pandas as pd
from io import StringIO
df = pd.read_csv(StringIO('qi_variable,qi_variable_type,valuenDEBIT_STUCCO_FT303A_KG_MIN,Time,2021-04-01 00:00:10nDEBIT_STUCCO_FT303A_KG_MIN,Time,2021-04-01 00:00:30nDEBIT_STUCCO_FT303A_KG_MIN,ValueY,"338,25"nDEBIT_STUCCO_FT303A_KG_MIN,ValueY,"337,799987792969"nDEBIT_EAU_MOUSSE_KG_MIN,Time,2021-04-01 00:00:10nDEBIT_EAU_MOUSSE_KG_MIN,Time,2021-04-01 00:00:30nDEBIT_EAU_MOUSSE_KG_MIN,ValueY,"55,1691627502441"nDEBIT_EAU_MOUSSE_KG_MIN,ValueY,"55,3335952758789"nCORRECTION_MOUSSE,Time,2021-04-01 00:04:12nCORRECTION_MOUSSE,Time,2021-04-01 00:04:35nCORRECTION_MOUSSE,ValueY,"1,04863631725311"nCORRECTION_MOUSSE,ValueY,"1,04946064949036"n'))
当前数据框df
:
预期结果:我试图将我的表格扩大到如下所示的结果。我试过了,pd.pivot
pd.pivot_table
但没有让它工作。忽略空白行,我将它们留空以提高可读性。
回答
单程:
df = df.pivot_table(index = 'qi_variable', columns = 'qi_variable_type', values = 'value', aggfunc= list).apply(pd.Series.explode)
选择:
df = df.pivot_table(index = ['qi_variable', df.groupby(['qi_variable','qi_variable_type']).cumcount()], columns = 'qi_variable_type', values = 'value', aggfunc= ''.join).reset_index(-1, drop=True)
另一种选择:
df = df.set_index(['qi_variable',df.groupby(['qi_variable', 'qi_variable_type']).cumcount(), 'qi_variable_type']).unstack(-1).reset_index(-1, drop=True)
输出:
qi_variable_type Time ValueY
qi_variable
CORRECTION_MOUSSE 2021-04-01 00:04:12 1,04863631725311
CORRECTION_MOUSSE 2021-04-01 00:04:35 1,04946064949036
DEBIT_EAU_MOUSSE_KG_MIN 2021-04-01 00:00:10 55,1691627502441
DEBIT_EAU_MOUSSE_KG_MIN 2021-04-01 00:00:30 55,3335952758789
DEBIT_STUCCO_FT303A_KG_MIN 2021-04-01 00:00:10 338,25
DEBIT_STUCCO_FT303A_KG_MIN 2021-04-01 00:00:30 337,799987792969