尝试创建一个熊猫df的词袋
Trying to create a bag of words of Panda's df
我是 pandas 的新手(也是 Python 的新手),我正在尝试为特定列的每一行创建一个词袋。 是我获取代码的地方,接下来是我的尝试:
for index, row in df.iterrows():
cell = df.Review2.iloc[index]
df['BOW'].iloc[index] = pd.Series([y for x in cell for y in x.split()]).value_counts()
这是我的数据框中的单个单元格,我想在其上执行上述操作(因此没有用于迭代所有行的 for 循环):
problem price say discount 6 bottle even show reduce check changesfive
star taste goodthis get best cabinet ever great crisp get best cabinet
ever great crisp originally buy three bottle wind buy whole case
holidaysnice california cab cab fantastic pleasantly surprise great
fullbodied flavor 1 cent ship promotion decent
非常感谢任何帮助!
import pandas as pd
from collections import Counter
df = pd.DataFrame({'review': ['Hello World Hello', 'Hi Bye Bye Bye']})
df['BOW'] = df.review.apply(lambda x: Counter(x.split(" ")))
review BOW
0 Hello World Hello {u'World': 1, u'Hello': 2}
1 Hi Bye Bye Bye {u'Bye': 3, u'Hi': 1}
我使用了 pandas apply 方法来处理所有行,而没有明确地迭代它们。
我是 pandas 的新手(也是 Python 的新手),我正在尝试为特定列的每一行创建一个词袋。
for index, row in df.iterrows():
cell = df.Review2.iloc[index]
df['BOW'].iloc[index] = pd.Series([y for x in cell for y in x.split()]).value_counts()
这是我的数据框中的单个单元格,我想在其上执行上述操作(因此没有用于迭代所有行的 for 循环):
problem price say discount 6 bottle even show reduce check changesfive star taste goodthis get best cabinet ever great crisp get best cabinet ever great crisp originally buy three bottle wind buy whole case holidaysnice california cab cab fantastic pleasantly surprise great fullbodied flavor 1 cent ship promotion decent
非常感谢任何帮助!
import pandas as pd
from collections import Counter
df = pd.DataFrame({'review': ['Hello World Hello', 'Hi Bye Bye Bye']})
df['BOW'] = df.review.apply(lambda x: Counter(x.split(" ")))
review BOW
0 Hello World Hello {u'World': 1, u'Hello': 2}
1 Hi Bye Bye Bye {u'Bye': 3, u'Hi': 1}
我使用了 pandas apply 方法来处理所有行,而没有明确地迭代它们。