如何使用自定义索引标签访问PANDAS DataFrame中的一行?
我已经创建了一个pandas dataframe来保存这样的数据:
TF_IDF = pd.DataFrame(index = vocab)
TF_IDF['0'] = 0
其中 vocab
是一系列字符串,因此每列的索引标签是相应的字符串,
所以当我打印它时,看起来像这样。 :
0
life 0
math 0
student 0
experi 0
control 0
... ..
slave 0
linga 0
31-32 0
democrat 0
unsustain 0
如何使用字符串以给定索引访问一行?
例如,如果我有字符串“数学”,一个人将如何使用索引标签“数学”访问值?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 而不是链接索引(如在您的答案中),因为它效率更高,链式索引有时可以提高
setterwithCopy
警告(阅读更多在这里)。要使用.loc,您将其称为下面:
以下是您的示例中的以下内容:
返回:
You should use
.loc[]
rather than chained indexing (as in your answer) as it's more efficient, and chained indexing can sometimes raise aSettingWithCopy
warning (read more here).To use .loc, you would call it like below:
Which would be the below in your example:
returns:
tf_idf ['0'] ['life']
会让您到达那里,而不是删除问题,我将把它留下来,以防万一其他人也找不到答案
TF_IDF['0']['life']
will get you thereInstead of removing the question, I'll leave it up, just in case someone else also cant find the answer anywhere else online