Ad

Dataframe Specific Transposition Optimisation

- 1 answer

I would like to transpose a Pandas Dataframe from row to columns, where number of rows is dynamic. Then, transposed Dataframe must have dynamic number of columns also.

I succeeded using iterrows() and concat() methods, but I would like to optimize my code.

Please find my current code:

import pandas as pd
expected_results_transposed = pd.DataFrame()
for i, r in expected_results.iterrows():
    t = pd.Series([r.get('B')], name=r.get('A'))
    expected_results_transposed = pd.concat([expected_results_transposed, t], axis=1)
print("CURRENT CASE EXPECTED RESULTS TRANSPOSED:\n{0}\n".format(expected_results_transposed))

Please find an illustration of expected result : picture of expected result

Do you have any solution to optimize my code using "standards" Pandas dataframes methods/options ?

Thank you for your help :)

Ad

Answer

Use DataFrame.transpose + DataFrame.set_index:

new_df=df.set_index('A').T.reset_index(drop=True)
new_df.columns.name=None

Example

  df2=pd.DataFrame({'A':'Mike Ana Jon Jenny'.split(),'B':[1,2,3,4]})
print(df2)

       A  B
0   Mike  1
1    Ana  2
2    Jon  3
3  Jenny  4

new_df=df2.set_index('A').T.reset_index(drop=True)
new_df.columns.name=None
print(new_df)

   Mike  Ana  Jon  Jenny
0     1    2    3      4
Ad
source: stackoverflow.com
Ad