You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
df1 = (df.set_index(['Animal', df.groupby('Animal').cumcount().add(1)])
.unstack()
.sort_index(axis=1, level=1))
df1.columns = [f'{a}_{b}' for a, b in df1.columns]
df1 = df1.reset_index()
print (df1)
Animal Age_1 Color_1 Length_1 Age_2 Color_2 Length_2 Age_3 Color_3 \
0 Cat 1 Brown 50cm 2 White 60cm 3 Brown
1 Dog 1 White 99cm 2 White 129cm 3 White
Length_3
0 55cm
1 105cm
If I want chain flatten MultIindex for one line solution, is it possible? Now f-strings cannot chain with sort_index and reset_index (or not idea how).
From SO:
If I want chain flatten
MultIindex
for one line solution, is it possible? Nowf-string
s cannot chain withsort_index
andreset_index
(or not idea how).Is possible implemented
MultiIndex.to_flat_index
forDataFrame
?The text was updated successfully, but these errors were encountered: