我有一个数据框架如下,
import pandas as pd
df= pd.DataFrame({'text':['The weather is nice','the house is amazing','the flowers are blooming']})
我想使用random.shuffle(),(e)对每一行中的单词进行洗牌。(新的第一行将是'nice is weather the'),所以我做了以下操作,
df.new_text = df.text.str.split()
并尝试映射或应用shuffle()函数,但它返回None。
print(df.new_text.map(lambda x: random.shuffle(x)))
或
print(df.new_text.apply(lambda x: random.shuffle(x)))
我不确定我在这里做错了什么。最后,我想把列表中打乱的单词连接起来,每行得到一个字符串,
df.new_text = df.new_text.apply( lambda x:' '.join(x))
可以了
shuffled_sentences = {"text":[]}
for sentence in df.values.ravel():
np.random.shuffle(sentence)
shuffled_sentences["text"].append(sentence)
shuffled_df = pd.DataFrame(shuffled_sentences)
np.random.shuffle
的问题是它不返回任何输出。因此,首先需要将要洗牌的列表存储在一个变量中。然后,如果对其应用np.random.shuffle
,则原始变量本身将被洗牌。
可以使用numpy库中的np.random.permutation
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
问题是random.shuffle
在适当的位置进行洗牌,并且不返回任何输出。您可以使用random.sample
:
df['new_text'] = df['text'].str.lower().str.split()
df['new_text'] = df['new_text'].apply(lambda x: random.sample(x, k=len(x)))
生成的值:
0 [is, weather, the, nice]
1 [amazing, house, is, the]
2 [are, blooming, flowers, the]
Name: new_text, dtype: object