我有一个数据帧,如下所示
df = pd.DataFrame({'text': ["Hi how are you","I am fine","I love you","I hate you"],
'tokens':[('Hi','how','are','you'),('I','am','fine'),('I','love','you'),('I','hate','you')]})我想要每个代币的pos标签。
for tok in df['tokens'].iterrows():
print(token, token.pos_)请注意,这里的pos_表示它是来自NLP域的语音标签的一部分
但是,我得到了一个错误
我可以告诉我如何迭代pandas列中的每一项吗?
发布于 2021-07-03 21:50:43
您得到'Series' object has no attribute 'iterrows'是因为您使用了列df['tokens'],它给出了一个序列(只有一维),所以没有'Series' object has no attribute 'iterrows'方法。
使用你的代码,你可以做到:
import pandas as pd
df = pd.DataFrame({'text': ["Hi how are you","I am fine","I love you","I hate you"],
'tokens':[('Hi','how','are','you'),('I','am','fine'),('I','love','you'),('I','hate','you')]})
for index, values in df.iterrows():
pos = 1
for x in values[1]:
print(pos, x)
pos += 1或者,您可以使用(类似于@AnuragDabas评论的内容):
df['pos tag'] = df['tokens'].apply(lambda x:list(range(len(x)+1))[1:])您只需要df.iat[2,1][1] ;)
https://stackoverflow.com/questions/68236197
复制相似问题