I'm trying to shuffle the values from an input dataframe, store these new values into a dictionary, and then get an output dataframe by replacing the input dataframe values with their dictionary mapping.
However, I get the "Replacement not allowed with overlapping keys and values" error.
Here's my code sample:
in_df = ['A','B','C']
in_df = pd.DataFrame(in_df,columns=['Alphabets'])
df_temp = in_df.sample(frac=1).reset_index(drop=True)
df_temp = df_temp.rename(columns={'Alphabets':'sample'})
mask_dict = dict(zip(in_df['Alphabets'], df_temp['sample']))
out_df= in_df.replace({'Alphabets': mask_dict})
in_df looks as follows:
Alphabets
A
B
C
mask_dict looks something like this:
{'A': 'C', 'B': 'A', 'C': 'C'}
I want the out_df to look like this:
Alphabets
C
A
C
I found a way to do this!
df_temp = in_df.stack().unique()
df_temp = pd.DataFrame(df_temp, columns=['Alphabets'])
df_temp1 = df_temp.sample(n=df_temp.size, random_state=123)
mask_dict = dict(zip(df_temp['Alphabets'], df_temp1['Alphabets']))
out_df = in_df.applymap(mask_dict.get)