Home > Back-end >  Create a new dataframe from a existing dataframe by iterating through repeated string values in colu
Create a new dataframe from a existing dataframe by iterating through repeated string values in colu

Time:12-29

Im reading in data and storing it in a pandas dataframe. I want to cretae a new dataframe structure.

Where Columns A and B are string values and column C are numpy floats.

Current dataframe structure (df_1):

Column A Column B Column C
Cell_1 Metal_1 0.2
Cell_1 Metal_2 0.4
Cell_1 Metal_3 0.3
Cell_1 Metal_4 0.5
Cell_2 Metal_1 0.1
Cell_2 Metal_2 0.2
Cell_2 Metal_3 0.3
Cell_2 Metal_4 0.7

I want to iterate through the current dataframe (df_1) and make a new dataframe (df_2).

So Column B string values become the index, Column B vlaues become individual columns and column C become rows associated with both.

The intended dataframe (df_2) will look like this:

Index Cell_1 Cell_2
Metal_1 0.2 0.1
Metal_2 0.4 0.2
Metal_3 0.3 0.3
Metal_4 0.5 0.7

I havent tried anything yet

CodePudding user response:

no need to use a loop. just use the pivot method

df.pivot(columns='Column A', values='Column C', index='Column B')
  • Related