0

I tackled the task of converting a pandas dataframe, which I loaded from a file, into the structure required for the next operations. While I was able to find a solution, I have a sense that it may not be optimal in terms of performance. Therefore, I am seeking clues or suggestions to improve it.

INPUT: dataframe

Required OUTPUT: dataframe

My code:

df = pd.read_csv(
                "out.csv",
                skiprows=[0, 1, 2]  # group header rows
            )

df_A = df[df['phase'] == "A"] 
df_A.set_index('_time', drop=True, inplace=True)
df_A.drop(columns=['phase'], inplace=True)
dict_names = { 'P':'PA', 'Q':'QA', 'S':'SA'}
df_A.rename(columns=dict_names, inplace=True)

df_B = df[df['phase'] == "B"] 
df_B.set_index('_time', drop=True, inplace=True)
df_B.drop(columns=['phase'], inplace=True)
dict_names = { 'P':'PB', 'Q':'QB', 'S':'SB'}
df_B.rename(columns=dict_names, inplace=True)

df_C = df[df['phase'] == "C"] 
df_C.set_index('_time', drop=True, inplace=True)
df_C.drop(columns=['phase'], inplace=True)
dict_names = { 'P':'PC', 'Q':'QC', 'S':'SC'}
df_C.rename(columns=dict_names, inplace=True)

out = pd.concat([df_A, df_B, df_C], axis=1)

I have attempted to create a solution using groupby, but I have not been able to make it work.

  • Can you try `df1 = df.pivot(index='_time', columns='phase')` and then `df1.columns = [f'{a}{b}' for a, b in df1.columns]` ? – jezrael Apr 03 '23 at 08:00

0 Answers0