匹配列表与数据框列中的项。

huangapple go评论68阅读模式
英文:

Find matches between a list and dataframe column

问题

我想要在pandas数据框中创建一个过滤器,并在数据框中打印特定值,比如如果所有项目都不可用,就打印"Failed"。

data.csv内容:

server,ip
server1,192.168.0.2
data,192.168.0.3
server3,192.168.0.100
server4,192.168.0.10

我创建了以下代码:

import pandas as pd

l = ['server1','server2']
ip_list = ['192.168.0.2','192.168.0.100','192.168.25.4']

data = pd.read_csv('data.csv')

data_filter = data[
    (data['ip'].isin(ip_list))
]

print(data_filter)

上述代码的输出如下:

    server             ip
0  server1    192.168.0.2
2  server3  192.168.0.100

我需要帮助以如下方式打印结果:

    server             ip   Status
0  server1    192.168.0.2   Success
2  server3  192.168.0.100   Success
3  NA         192.168.25.4  Failed

请指导我如何创建这段代码?

英文:

I want to create a filter in pandas dataframe and print specific values like failed if all items are not available in dataframe.

data.csv content:

server,ip
server1,192.168.0.2
data,192.168.0.3
server3,192.168.0.100
server4,192.168.0.10

I created below code:

import pandas as pd

l = ['server1','server2']
ip_list = ['192.168.0.2','192.168.0.100','192.168.25.4']

data = pd.read_csv('data.csv')
#count = ip_list.count()


data_filter = data[
    (data['ip'].isin(ip_list) ) 
]

print(data_filter)

Above code is printing result as below:

    server             ip
0  server1    192.168.0.2
2  server3  192.168.0.100

I need help to print result as below

    server             ip   Status
0  server1    192.168.0.2    Success
2  server3  192.168.0.100   Success
3  NA         192.168.25.4  Failed

Please guide me how can I create this code?

答案1

得分: 0

使用pandas.DataFrame.mergepandas.Series.map函数:

df_filtered = (df.merge(pd.DataFrame(ip_list, columns=['ip']), 
                       on='ip', how='right', indicator='Status'))
df_filtered['Status'] = df_filtered['Status'].map({'both': 'Success', 'right_only': 'Failed'})

结果如下:

    server             ip   Status
0  server1    192.168.0.2  Success
1  server3  192.168.0.100  Success
2      NaN   192.168.25.4   Failed
英文:

With pandas.DataFrame.merge + pandas.Series.map functions:

df_filtered = (df.merge(pd.DataFrame(ip_list, columns=['ip']), 
                       on='ip', how='right', indicator='Status'))
df_filtered['Status'] = df_filtered['Status'].map({'both': 'Success', 'right_only': 'Failed'})

    server             ip   Status
0  server1    192.168.0.2  Success
1  server3  192.168.0.100  Success
2      NaN   192.168.25.4   Failed

答案2

得分: 0

This should do the trick:

import pandas as pd

l = ['server1', 'server2']
ip_list = ['192.168.0.2', '192.168.0.100', '192.168.25.4']

data = pd.read_csv('data.csv')
#count = ip_list.count()

data['Status'] = list(map(lambda x: 'Success' if x else 'Failed', data['ip'].isin(ip_list)))

print(data)
英文:

This should do the trick:

import pandas as pd

l = ['server1','server2']
ip_list = ['192.168.0.2','192.168.0.100','192.168.25.4']

data = pd.read_csv('data.csv')
#count = ip_list.count()


data['Status'] = list(map(lambda x: 'Success' if x else 'Failed', data['ip'].isin(ip_list)))

print(data)

huangapple
  • 本文由 发表于 2023年4月11日 00:07:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定