pandas:将 random.shuffle() 应用于列表列

pandas: apply random.shuffle() to list column

我有如下数据框,

import pandas as pd
df= pd.DataFrame({'text':['The weather is nice','the house is amazing','the flowers are blooming']})

我想使用 random.shuffle() 打乱每一行中的单词,(例如新的第一行将是 'nice is weather the' ),所以我做了以下操作,

df.new_text = df.text.str.split()

并尝试映射或应用 shuffle() 函数,但它 returns 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 的问题是它 return 没有任何输出。因此,您需要先将要洗牌的列表存储在一个变量中。然后,如果您对其应用 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 does the shuffle in place and does not return any output. You can use 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