我一直在寻找,但没有找到答案。我有下一个数据框
Pais Anio Electricidad Electricidad Electricidad Electricidad Electricidad Electricidad Electricidad Electricidad Electricidad Electricidad Electricidad
0 NaN NaN No No No No No Si Si Si Si Si Total
1 NaN NaN Rural Rural Urbana Urbana Total No Rural Rural Urbana Urbana Total Si Total Si
2 NaN NaN Hombre Mujer Hombre Mujer NaN Hombre Mujer Hombre Mujer NaN NaN
3 Argentina 2015 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
4 Bolivia 2014 513160 462745 24457 25959 1026321 1187340 1243921 3554853 3686894 9673008 10699329
5 Brasil 2015 287373 216447 28718 15895 548433 15898153 14545231 81355185 88432517 200231086 200779519
6 Chile 2011 20192 16702 8752 7090 52736 1054604 1053353 6936960 7749581 16794498 16847234
这是所需的输出:
我将所需的输出放在图像中,因为它有很多数据,我设法只得到一个融化,但我还需要“融化”是/否,区域和性别行......
我的代码是这样的:
df1 = df.iloc[0:3] # select three first rows
df1 = df1.ffill(axis='columns') #Rellenando los grupos
df = df.iloc[3:]
# my_dataframe = my_dataframe[my_dataframe.employee_name != 'chad']
df1 = df1.append(df)
# moviendo primera fila a titulo de columna
df1.columns = df1.iloc[0]
df1 = df1.reindex(df1.index.drop(0)).reset_index(drop=True)
df1.columns.name = None
df1 = pd.melt(df1, id_vars=["Pais", "Anio"], var_name="Pregunta")
对此的建议和意见表示赞赏!!!