简体   繁体   English

基于 if 条件 pandas 从仅列中提取所有值作为列表

[英]Extracting all the values from ONLY a column as list based on an if condition pandas

Suppose I have a dataframe with three columns:假设我有一个包含三列的 dataframe:

Host    DB    Status
001     A     ONLINE
001     B     ONLINE
001     C     OFFLINE
001     D     OFFLINE

I want list of all DB's which have status as offline.我想要所有状态为脱机的数据库的列表。 I cant do loop as I am getting this data in a loop for each server and at the same time after getting I need to clean it.我不能循环,因为我在每个服务器的循环中获取这些数据,同时在获取后我需要清理它。

In output I want [C,D]在 output 我想要[C,D]

I tried this:我试过这个:

temp_df.loc[temp_df.STATE=="OFFLINE","Status"]["DB"]

But this is not working但这不起作用

Try this:尝试这个:

print(df[df['Status']=='OFFLINE']['DB'].tolist())
['C', 'D']

Fix your code修复你的代码

temp_df.loc[temp_df.Status=="OFFLINE","DB"].to_numpy()

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

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