After some aggregations and other manipulation, I get a pandas df that looks like this:
However, I am trying to get the same info into a different format, that combines all the info in a single row, with columns names like this:
cols = [amin_qcut0, amax_qcut0, mean_qcut0, amin_qcut1, amax_qcut1, mean_qcut1, amin_qcut2, amax_qcut2, mean_qcut2]
and the values corresponding to the values in the table. Is there a way to do this w/o using df.iterrows() ?
Thanks
Try this, there isn't a need to iterate use dataframe reshaping:
#Create dummy dataframe
df = pd.DataFrame(np.arange(9).reshape(-1,3), columns=['amin','amax','mean'])
df.index.name='qcut'
#Use unstack to reshape dataframe with T from transpose
df_out = df.unstack().to_frame().T.sort_index(level=1, axis=1)
#Flatten multiindex column header created from unstack
df_out.columns = [f'{i}_{j}' for i, j in df_out.columns]
#Show output
df_out
Output:
amax_0 amin_0 mean_0 amax_1 amin_1 mean_1 amax_2 amin_2 mean_2
0 1 0 2 4 3 5 7 6 8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.