Python 3.x 如何通过Python3在pandas中对列表表单中的每个列进行配对 df=pd.DataFrame([['an apple is red','pop is here',['pear is green','see is blue']],columns=['A','B'] 从nltk.tokenize导入TweetTokenizer df['A']=[TweetTokenizer()。为df['A']]中的文本标记(文本) df['id']=[1,2] 对于df['A']中的k: 印刷品(k) pid=df[df['A']==k]['id']。值[0] [‘安’、‘苹果’、‘是’、‘红色’] --------------------------------------------------------------------------- ValueError回溯(最近一次调用上次) 在里面 1表示df['A']中的k: 2份印刷品(k) ---->3 pid=df[df['A']==k]['id']。值[0] 包装器中的~/Library/Python/3.7/lib/Python/site-packages/pandas/core/ops.py(self、other、axis) 1743年#将播出 1744如果other.ndim!=0且len(self)!=len(other): ->1745 raise VALUE ERROR('长度必须匹配才能进行比较') 1746 1747 res_值=na_op(自值,np.asarray(其他)) ValueError:长度必须匹配才能进行比较
我想得到当k等于A列的每一行时的pid。 当每一行都是字符串时,我可以完成。当每一行都是列表时,如何完成 匹配后,我得到相应的'id'值 预期pid输出: 1.Python 3.x 如何通过Python3在pandas中对列表表单中的每个列进行配对 df=pd.DataFrame([['an apple is red','pop is here',['pear is green','see is blue']],columns=['A','B'] 从nltk.tokenize导入TweetTokenizer df['A']=[TweetTokenizer()。为df['A']]中的文本标记(文本) df['id']=[1,2] 对于df['A']中的k: 印刷品(k) pid=df[df['A']==k]['id']。值[0] [‘安’、‘苹果’、‘是’、‘红色’] --------------------------------------------------------------------------- ValueError回溯(最近一次调用上次) 在里面 1表示df['A']中的k: 2份印刷品(k) ---->3 pid=df[df['A']==k]['id']。值[0] 包装器中的~/Library/Python/3.7/lib/Python/site-packages/pandas/core/ops.py(self、other、axis) 1743年#将播出 1744如果other.ndim!=0且len(self)!=len(other): ->1745 raise VALUE ERROR('长度必须匹配才能进行比较') 1746 1747 res_值=na_op(自值,np.asarray(其他)) ValueError:长度必须匹配才能进行比较,python-3.x,pandas,list,Python 3.x,Pandas,List,我想得到当k等于A列的每一行时的pid。 当每一行都是字符串时,我可以完成。当每一行都是列表时,如何完成 匹配后,我得到相应的'id'值 预期pid输出: 1. 2我对您的代码做了一些更改,它根据您的问题正确返回pid df = pd.DataFrame([['an apple is red', 'pop is here'],['pear is green', 'see is blue']], columns=['A', 'B'] from nltk.tokenize import Tweet
2我对您的代码做了一些更改,它根据您的问题正确返回pid
df = pd.DataFrame([['an apple is red', 'pop is here'],['pear is green', 'see is blue']], columns=['A', 'B']
from nltk.tokenize import TweetTokenizer
df['A'] = [TweetTokenizer().tokenize(text) for text in df['A']]
df['id']=[1,2]
for k in df['A']:
print(k)
pid = df[df['A']==k]['id'].values[0]
['an', 'apple', 'is', 'red']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-156-f9e4fa5143b0> in <module>
1 for k in df['A']:
2 print(k)
----> 3 pid = df[df['A']==k]['id'].values[0]
~/Library/Python/3.7/lib/python/site-packages/pandas/core/ops.py in wrapper(self, other, axis)
1743 # as it will broadcast
1744 if other.ndim != 0 and len(self) != len(other):
-> 1745 raise ValueError('Lengths must match to compare')
1746
1747 res_values = na_op(self.values, np.asarray(other))
ValueError: Lengths must match to compare
由于list
比较在dataframe
中比较混乱,因此最好将列值转换为tuple
,然后进行比较
如果您只需要在此处对每一行进行索引,还可以在迭代时查看获取
dataframe
行的索引。您可以移植您的预期输出吗?您的df['A']
是一个系列0[an,apple,is,red]1[pear,is,green]
使用列表。您需要再次遍历列表以获取列表中的每个项目。这是您所期望的吗?您可以通过将列值转换为如下字符串进行比较:df[df['A'].str.contains(k)]
或df.loc[df['A']==k]
?@UserAG我得到了第一个方法:TypeError:unhable type:'list'和第二个方法:ValueError:length必须匹配才能进行比较解释您尝试执行的操作
df = pd.DataFrame([['an apple is red', 'pop is here'],['pear is green', 'see is blue']], columns=['A', 'B'])
from nltk.tokenize import TweetTokenizer
df['A'] = [TweetTokenizer().tokenize(text) for text in df['A']]
df['id']=[1,2]
for k in df['A']:
print(k)
pid = df[df['A'].map(tuple)==tuple(k)]['id'].values[0]
print(pid)