Shuffle one column in pandas dataframe
This also appears to do the job:
df1['HS_FIRST_NAME'] = df[4].sample(frac=1).values
The immediate error is a symptom of using an inadvisable approach when working with dataframes.
np.random.shuffle
works in-place and returns None
, so assigning to the output of np.random.shuffle
will not work. In fact, in-place operations are rarely required, and often yield no material benefits.
Here, for example, you can use np.random.permutation
and use NumPy arrays via pd.Series.values
rather than series:
if devprod == 'prod':
#do not shuffle data
df1['HS_FIRST_NAME'] = df[4]
df1['HS_LAST_NAME'] = df[6]
df1['HS_SSN'] = df[8]
else:
df1['HS_FIRST_NAME'] = np.random.permutation(df[4].values)
df1['HS_LAST_NAME'] = np.random.permutation(df[6].values)
df1['HS_SSN'] = np.random.permutation(df[8].values)