I have a dataframe as follows,
import pandas as pd
df= pd.DataFrame({'text':['The weather is nice','the house is amazing','the flowers are blooming']})
I would like to shuffle the words in each row using random.shuffle(),(e.g the new first row will be 'nice is weather the' ),so I have done the following,
df.new_text = df.text.str.split()
and tried to map or apply shuffle() function but it returns None.
print(df.new_text.map(lambda x: random.shuffle(x)))
or
print(df.new_text.apply(lambda x: random.shuffle(x)))
I am not sure what I am doing wrong here. and then finally I would like to join the shuffled words in the list to get a string per row,
df.new_text = df.new_text.apply( lambda x:' '.join(x))
CodePudding user response:
This does the job.
shuffled_sentences = {"text":[]}
for sentence in df.values.ravel():
np.random.shuffle(sentence)
shuffled_sentences["text"].append(sentence)
shuffled_df = pd.DataFrame(shuffled_sentences)
The thing with np.random.shuffle
is that it doesn't return any output. So you need to store the list you want to shuffle in a vraible first. Then if you apply np.random.shuffle
on it, the original variable itself would be shuffled.
CodePudding user response:
You can use np.random.permutation
from numpy library
df= pd.DataFrame({'text':['The weather is nice','the house is amazing','the
flowers are blooming']})
df['new_text']= df['text'].apply(lambda x:x.split())
df['new_text']= df['new_text'].map(lambda x: np.random.permutation(x))
df['new_text']= df['new_text'].apply( lambda x:' '.join(x))
display(df.new_text)
0 nice weather is The
1 the is house amazing
2 flowers are the blooming
CodePudding user response:
The problem is that random.shuffle
does the shuffle in place and does not return any output. You can use random.sample
instead:
df['new_text'] = df['text'].str.lower().str.split()
df['new_text'] = df['new_text'].apply(lambda x: random.sample(x, k=len(x)))
Resulting values:
0 [is, weather, the, nice]
1 [amazing, house, is, the]
2 [are, blooming, flowers, the]
Name: new_text, dtype: object