Convert concatenate column and row values and assigned to New column after groupby one column in the dataframe.
The below code trying to convert the expected results however it taking longer time to run the results.
code:
groups = df.groupby("row_number")df_new = groups.apply(func).droplevel(0)def func(g: pd.DataFrame) -> pd.DataFrame: first_row_of_group = g.iloc[0] g = g.iloc[1:, :] cols = range(1, len(first_row_of_group)) for i in cols: g["structure_data"] = ( g["structure_data"]+ str(first_row_of_group.iloc[i])+"="+ g.iloc[:, i].apply(try_round)+";" ) return gDataFrame:
| Column A | Column B | Column C | Column D | Column E | structure_data |
|---|---|---|---|---|---|
| A | 0D | 1D | 7D | 14D | None |
| A | 0.03891 | 0.0410 | 0.0417 | 0.0435 | None |
| A | 0.03891 | 0.0410 | 0.0417 | 0.0435 | None |
| A | 0.03891 | 0.0410 | 0.0417 | 0.0435 | None |
| A | 0.03891 | 0.0410 | 0.0417 | 0.0435 | None |
Expected Value in structure_data column:
| Column A | Column B | Column C | Column D | Column E | structure_data |
|---|---|---|---|---|---|
| A | 0.03891 | 0.0410 | 0.0417 | 0.0435 | 0D=0.03891;1D=0.0410;7D=0.0417;14D=0.0435 |
| A | 0.03891 | 0.0410 | 0.0417 | 0.0435 | 0D=0.03891;1D=0.0410;7D=0.0417;14D=0.0435 |
| A | 0.03891 | 0.0410 | 0.0417 | 0.0435 | 0D=0.03891;1D=0.0410;7D=0.0417;14D=0.0435 |
| A | 0.03891 | 0.0410 | 0.0417 | 0.0435 | 0D=0.03891;1D=0.0410;7D=0.0417;14D=0.0435 |