I have a dataframe as follows,
JavaScript
x
3
1
import pandas as pd
2
df= pd.DataFrame({'text':['The weather is nice','the house is amazing','the flowers are blooming']})
3
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,
JavaScript
1
2
1
df.new_text = df.text.str.split()
2
and tried to map or apply shuffle() function but it returns None.
JavaScript
1
2
1
print(df.new_text.map(lambda x: random.shuffle(x)))
2
or
JavaScript
1
2
1
print(df.new_text.apply(lambda x: random.shuffle(x)))
2
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,
JavaScript
1
2
1
df.new_text = df.new_text.apply( lambda x:' '.join(x))
2
Advertisement
Answer
This does the job.
JavaScript
1
8
1
shuffled_sentences = {"text":[]}
2
3
for sentence in df.values.ravel():
4
np.random.shuffle(sentence)
5
shuffled_sentences["text"].append(sentence)
6
7
shuffled_df = pd.DataFrame(shuffled_sentences)
8
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.