我在数据框中有字符串列,在这里将句子拆分为单词。现在,我需要计算该单词的出现并将其转换为列。基本上创建一个文档术语矩阵
0 [kubernetes, client, bootstrapping, ponda]
1 [micro, insu]
2 [motor, upi]
3 [secure, app, installation]
4 [health, insu, express, credit, customer]
5 [secure, app, installation]
6 [aap, insta]
7 [loan, house, loan, customers]
输出:
kubernetes client bootstrapping ponda loan customers installation
0 1 1 1 1 0 0 0
1 0 0 0 0 1 0 1
2 0 2 0 0 0 0 0
3 1 1 1 1 0 0 0
到目前为止的代码
from sklearn.feature_extraction.text import CountVectorizer
countvec = CountVectorizer()
countvec.fit_transform(df.new)
错误:
AttributeError:“ list”对象没有属性“ lower”
答案 0 :(得分:1)
要以CountVectorizer
的方式使用它,您需要使DataFrame像这样:
string
0 kubernetes client bootstrapping ponda
1 micro insu
2 motor upi
3 secure app installation
4 health insu express credit customer
5 secure app installation
6 aap insta
7 loan house loan customers
此刻,您将拥有以下内容:
stringList
0 [kubernetes, client, bootstrapping, ponda]
1 [micro, insu]
2 [motor, upi]
3 [secure, app, installation]
4 [health, insu, express, credit, customer]
5 [secure, app, installation]
6 [aap, insta]
7 [loan, house, loan, customers]
因此,您将按照使用CountVectorizer
以下是可重现的示例:
df = pd.DataFrame([[['kubernetes', 'client', 'bootstrapping', 'ponda']], [['micro', 'insu']], [['motor', 'upi']],[['secure', 'app', 'installation']],[['health', 'insu', 'express', 'credit', 'customer']],[['secure', 'app', 'installation']],[['aap', 'insta']],[['loan', 'house', 'loan', 'customers']]])
df.columns = ['new']
我正在呼叫您的列,该列的单词列表像new
一样,就像它最初在您的DataFrame中一样。
df['string'] = ""
我正在创建一个空列,在这里我将连接单词列表中的每个单词。
for i in df.index:
df.at[i, 'string'] = " ".join(item for item in df.at[i, 'new'])
我已经按行进行了扫描,并用" "
将字符串列表中的每个项目串联起来,并将其添加到string
列中。
df.drop(['new'], axis = 1, inplace = True)
现在,不需要带有字符串列表的列!所以我放弃了。
现在您的DataFrame可以按照您想要的方式准备好了!现在您可以使用CountVectorizer
!
from sklearn.feature_extraction.text import CountVectorizer
countvec = CountVectorizer()
counts = countvec.fit_transform(df['string'])
vocab = pd.DataFrame(counts.toarray())
vocab.columns = countvec.get_feature_names()
print(vocab)
给予
aap app bootstrapping client credit customer customers express \
0 0 0 1 1 0 0 0 0
1 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
3 0 1 0 0 0 0 0 0
4 0 0 0 0 1 1 0 1
5 0 1 0 0 0 0 0 0
6 1 0 0 0 0 0 0 0
7 0 0 0 0 0 0 1 0
health house insta installation insu kubernetes loan micro motor \
0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 1 0 0 1 0
2 0 0 0 0 0 0 0 0 1
3 0 0 0 1 0 0 0 0 0
4 1 0 0 0 1 0 0 0 0
5 0 0 0 1 0 0 0 0 0
6 0 0 1 0 0 0 0 0 0
7 0 1 0 0 0 0 2 0 0
ponda secure upi
0 1 0 0
1 0 0 0
2 0 0 1
3 0 1 0
4 0 0 0
5 0 1 0
6 0 0 0
7 0 0 0
答案 1 :(得分:0)
如果值是列表,首先将它们join
一起列出,然后使用CountVectorizer
:
print (type(df.loc[0, 'new']))
<class 'list'>
from sklearn.feature_extraction.text import CountVectorizer
countvec = CountVectorizer()
counts = countvec.fit_transform(df['new'].str.join(' '))
df = pd.DataFrame(counts.toarray(), columns=countvec.get_feature_names())
使用get_dummies
和sum
的另一种熊猫解决方案:
df1 = pd.DataFrame(df['new'].values.tolist())
df = pd.get_dummies(df1, prefix='', prefix_sep='').sum(axis=1, level=0)
print (df)
aap app bootstrapping client credit customer customers express \
0 0 0 1 1 0 0 0 0
1 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
3 0 1 0 0 0 0 0 0
4 0 0 0 0 1 1 0 1
5 0 1 0 0 0 0 0 0
6 1 0 0 0 0 0 0 0
7 0 0 0 0 0 0 1 0
health house insta installation insu kubernetes loan micro motor \
0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 1 0 0 1 0
2 0 0 0 0 0 0 0 0 1
3 0 0 0 1 0 0 0 0 0
4 1 0 0 0 1 0 0 0 0
5 0 0 0 1 0 0 0 0 0
6 0 0 1 0 0 0 0 0 0
7 0 1 0 0 0 0 2 0 0
ponda secure upi
0 1 0 0
1 0 0 0
2 0 0 1
3 0 1 0
4 0 0 0
5 0 1 0
6 0 0 0
7 0 0 0