Python 如何将特定列中的单词作为标签分配给新的数据帧
嗨,朋友,我是新来的所以我稍微修改了你的代码,你的步骤3如下所示:Python 如何将特定列中的单词作为标签分配给新的数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,嗨,朋友,我是新来的所以我稍微修改了你的代码,你的步骤3如下所示: # 3- Countung the seprated words and the frequency of repetation df_word_count=pd.DataFrame(df.A.str.split(' ').explode().value_counts()).reset_index().rename({'index':"A","A":"Count"},a
# 3- Countung the seprated words and the frequency of repetation
df_word_count=pd.DataFrame(df.A.str.split(' ').explode().value_counts()).reset_index().rename({'index':"A","A":"Count"},axis=1)
display(df_word_count)
list_word_count=list(df_word_count["A"])
len(list_word_count)
最大的变化是list\u word\u count=list(df\u word\u count[“a]”)中变量的名称。
代码的其余部分与新变量类似:
# 4- Make a ZERO-Matrix
allfeatures=np.zeros((df.shape[0],len(list_word_count)))
allfeatures.shape
# 5- Make a For-Loop
for i in range(len(list_word_count)):
allfeatures[:,i]=df['A'].agg(lambda x:x.split().count(list_word_count[i]))
# 6- Concat the data
Complete_data=pd.concat([df,pd.DataFrame(allfeatures)],axis=1)
display(Complete_data)
唯一的变化是变量的不同名称。我要做的是第七步
# 7- change columns name from list
#This creates a list of the words you wanted
l = list(df_word_count["A"])
# if you see this, it shows only the words you have in the column A
# but the result dataset that you showed you wanted, you also had some columns #that had values such as word count, etc. So we need to add that. We do this by #inserting those values you want in the list, at the beginning
l.insert(0,"char_count")
l.insert(0,"word_count")
l.insert(0,"A")
# Finally, I rename all the columns with the names that I have in the list l
Complete_data.columns = l
我明白了:
谢谢你的支持,但我不知道该怎么办。你能一步一步地告诉我你做了什么吗?我编辑过,希望更清楚