0

I have a data frame with 3 columns.

df = pd.DataFrame({'Cont_ID':[10,10,20,20,30,30],\
'KEY':['450,512','405,512','450,512','405,512','450,512','405,512'],\
'VALUE': [3,12,5,23,7,100]})

   Cont_ID  KEY          VALUE
0  10       '450,512'    3
1  10       '405,512'    12
2  20       '450,512'    5
3  20       '405,512'    23
4  30       '450,512'    7
5  30       '405,512'    100

In this dataset each cont_ID represents a unique item that has multiple values associated with it. I need to split those values into separate columns.

I need to create 2 new columns. One needs to hold the value for each plates' associated '405,512' value. The second new column needs to hold the plates associated '450,512' value. The final table would look something like this:

   Cont_ID  "450-512"  "405-512"
0  10       3          12
1  20       5          23
2  30       7          100

I have tried to do this through several methods, examples listed below, but I am pretty new to both python and pandas, so im not sure where I am going wrong.

df_split = lambda x: x['VALUE'] if x['KEY'] == "450,512"
df['450-512'] = df.apply(df_split, axis = 1)

df['450-512'] = df[x['VALUE'] for x in df if df['KEY'] == ['450,512']]

df['450-512'] = df['VALUE'].str.contains('450,512).astype(str)

Any help would be much appreciated!

0 Answers0