简体   繁体   English

pandas 查询单输出

[英]pandas query single output

I have a pandas dataframe (called smalls) that is repurposed several times to create several network diagrams from a dataset.我有一个 Pandas 数据框(称为 smalls),它被多次重新利用以从数据集创建多个网络图。 I am trying to set the color of the nodes in one of the diagrams based on entity type and need to query the original dataframe.我正在尝试根据实体类型在其中一个图表中设置节点的颜色,并且需要查询原始数据帧。 However, when I do so it results in a series, which I then cannot perform a comparison on.但是,当我这样做时,它会产生一个系列,然后我无法对其进行比较。 How can I modify the first line below to only give me the first entry from the dataframe (all of the others will be the same)?如何修改下面的第一行,只给我数据框中的第一个条目(所有其他条目都相同)?

temp=smalls.Role[smalls.Entity==big_nodes_order[i]]

print(temp)
10    Threat
11    Threat
12    Threat
Name: Role, dtype: object

I think you can use iloc or iat :我认为您可以使用ilociat

temp=smalls.Role[smalls.Entity==big_nodes_order[i]]
print temp
10    Threat
11    Threat
12    Threat
Name: Role, dtype: object

print temp.iloc[0]
Threat

print temp.iat[0]
Threat

print temp.iloc[:1]
10    Threat
Name: Role, dtype: object

或者,您可以使用.head()方法:

temp=smalls.Role[smalls.Entity==big_nodes_order[i]].head(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM