>AttributeError: 'list' object has no attribute 'lower' (in a lowercase dataframe)
>AttributeError: 'list' object has no attribute 'lower' (in a lowercase dataframe)
我不明白这个错误...我在把它变成列表之前已经把df变成了小写
数据框:
all_cols
0 who is your hero and why
1 what do you do to relax
2 this is a hero
4 how many hours of sleep do you get a night
5 describe the last time you were relax
代码:
from sklearn.cluster import MeanShift
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
df['all_cols'] = df['all_cols'].str.lower()
df_list = df.values.tolist()
pipeline = Pipeline(steps=[
('tfidf', TfidfVectorizer()),
('trans', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
('clust', MeanShift())])
pipeline.fit(df_list)
pipeline.named_steps['clust'].labels_
result = [(label,doc) for doc,label in zip(df_list, pipeline.named_steps['clust'].labels_)]
for label,doc in sorted(result):
print(label, doc)
但是我在这一行有一个错误:
AttributeError Traceback (most recent call last)
in
----> 1 pipeline.fit(df_list)
2 pipeline.named_steps['clust'].labels_
AttributeError: 'list' object has no attribute 'lower'
但是,如果我之前已经传递了小写数据帧,为什么程序会返回小写错误?
为 df_list
指定的列以避免嵌套列表:
df_list = df.values.tolist()
print (df_list)
[['who is your hero and why'],
['what do you do to relax'],
['this is a hero'],
['how many hours of sleep do you get a night'],
['describe the last time you were relax']]
df_list = df['all_cols'].values.tolist()
print (df_list)
['who is your hero and why',
'what do you do to relax',
'this is a hero',
'how many hours of sleep do you get a night',
'describe the last time you were relax']
将其转换为pandas数据框,然后进行上述操作。它会起作用。
我仍然粘贴了代码片段,你可以自己试试。
import pandas as pd
col = pd.Series(["who is your hero and why", "what do you do to relax", "this is a hero", "how many hours of sleep do you get a night", "describe the last time you were relax"])
fr = {"all_cols":col}
df = pd.DataFrame(fr)
df['all_cols'] = df['all_cols'].str.lower()
df_list = df.values.tolist()
我不明白这个错误...我在把它变成列表之前已经把df变成了小写
数据框:
all_cols
0 who is your hero and why
1 what do you do to relax
2 this is a hero
4 how many hours of sleep do you get a night
5 describe the last time you were relax
代码:
from sklearn.cluster import MeanShift
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
df['all_cols'] = df['all_cols'].str.lower()
df_list = df.values.tolist()
pipeline = Pipeline(steps=[
('tfidf', TfidfVectorizer()),
('trans', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)),
('clust', MeanShift())])
pipeline.fit(df_list)
pipeline.named_steps['clust'].labels_
result = [(label,doc) for doc,label in zip(df_list, pipeline.named_steps['clust'].labels_)]
for label,doc in sorted(result):
print(label, doc)
但是我在这一行有一个错误:
AttributeError Traceback (most recent call last) in
----> 1 pipeline.fit(df_list)
2 pipeline.named_steps['clust'].labels_
AttributeError: 'list' object has no attribute 'lower'
但是,如果我之前已经传递了小写数据帧,为什么程序会返回小写错误?
为 df_list
指定的列以避免嵌套列表:
df_list = df.values.tolist()
print (df_list)
[['who is your hero and why'],
['what do you do to relax'],
['this is a hero'],
['how many hours of sleep do you get a night'],
['describe the last time you were relax']]
df_list = df['all_cols'].values.tolist()
print (df_list)
['who is your hero and why',
'what do you do to relax',
'this is a hero',
'how many hours of sleep do you get a night',
'describe the last time you were relax']
将其转换为pandas数据框,然后进行上述操作。它会起作用。 我仍然粘贴了代码片段,你可以自己试试。
import pandas as pd
col = pd.Series(["who is your hero and why", "what do you do to relax", "this is a hero", "how many hours of sleep do you get a night", "describe the last time you were relax"])
fr = {"all_cols":col}
df = pd.DataFrame(fr)
df['all_cols'] = df['all_cols'].str.lower()
df_list = df.values.tolist()