I am trying to shuffle data within the same group in a dataframe(grouping 4 columns together) using pandas and numpy as shown below.
The grouped columns are ID, values, source, type and function. I am have copied the same values from the ‘value’ column into the ‘values_new_column’ and would like to shuffle within this new column ‘values_new_column’ (image attached).
So far, I have written this code:
pdf1["values_new_column"] = pdf1.groupby("values")['source','type', 'function', 'ID'].transform(np.random.permutation)
The error says that the current library says that indexing with multiple keys will be deprecated and has asked me to use a list instead. Is there any way I can shuffle the records within one column inside multiple grouped pandas columns?
Expected output: enter image description here
1