Skip to content
Advertisement

Serach List names in a dataframe column pandas

I am trying to match my list of server with the pandas dataframe in the column Server Name if the name in the list matches in the Server Name then print the entire row.

there are chances names is the my_List do not match entirely like one of the server name in my_List is tick1001.example.us.com while in Server Name.

This this my kind of list

my_List = ['tick1001.example.us.comn', 'tick2001.example.us.comn', 'tick1003.example.us.comn']

Below is the Data Sample.

 Server         Server Name             Model                   Processor                          Proc. Count  Memory  Serial Number   iLO iLO FW  Firmware    Appliance Name  VIC
enc2021, bay 1  tick2041.example.us.com ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   524288  2M272300GJ  1. 10.127.3.31  2.53 May 03 2017    I36 v2.40 (02/17/2017)  OV C7000 enclosures 1
ENC2013, bay 1  tpc2111.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   524288  2M2708026H  1. 10.127.1.198 2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
enc1018, bay 1  tpc1173.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   262144  2M272101PR  1. 10.127.2.143 2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
ENC1002, bay 1  tpc1012.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz   2   524288  2M26250531  1. 10.127.0.109 2.40 Dec 02 2015    I36 v2.00 (12/28/2015)  OV C7000 enclosures 1
ENC1005, bay 1  tick1003.example.us.com ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz   2   524288  2M262505VB  1. 10.127.0.70  2.62 Jan 09 2019    I36 v2.72 (03/25/2019)  OV C7000 enclosures 1
ENC2003, bay 1  tpc2023.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz   2   524288  2M262602L6  1. 10.127.0.234 2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
enc2015, bay 1  tpc2141.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   262144  2M272101ML  1. 10.127.2.173 2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
enc2016, bay 1  tpc2156.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   262144  2M272101PJ  1. 10.127.2.188 2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
enc1013, bay 1  tpc1098.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   262144  2M272101N0  1. 10.127.2.68  2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
enc2019, bay 1  tpc2201.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   262144  2M272101NQ  1. 10.127.2.233 2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
ENC2005, bay 1  tick2001    ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   524288  2M26360507  1. 10.127.0.78  2.62 Jan 09 2019    I36 v2.72 (03/25/2019)  OV C7000 enclosures 1
ENC2006, bay 1  tick1001    ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   524288  2M263604ZZ  1. 10.127.0.58  2.40 Dec 02 2015    I36 v2.20 (05/05/2016)  OV C7000 enclosures 1
ENC2009, bay 1  tpc2060.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz   2   393216  2M263800Y4  1. 10.127.1.68  2.70 May 07 2019    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
enc1025, bay 1  tpc1250.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2667 v3 @ 3.20GHz   2   393216  2M26100687  1. 10.127.3.231 2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1
ENC2008, bay 1  tpc2045.example.us.com  ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   524288  2M264800VR  1. 10.127.1.53  2.55 Aug 16 2017    I36 v2.52 (10/25/2017)  OV C7000 enclosures 1

Below is my code tried:

import pandas as pd
my_List = open("vic_hosts").readlines()

df = pd.read_csv('report.csv')
df = [df['Server Name'].isin(my_List)]
print(df)

Expected:

Server          Server Name             Model                   Processor                          Proc. Count  Memory  Serial Number   iLO iLO FW  Firmware    Appliance Name  VIC
ENC1005, bay 1  tick1003.example.us.com ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz   2   524288  2M262505VB  1. 10.127.0.70  2.62 Jan 09 2019    I36 v2.72 (03/25/2019)  OV C7000 enclosures 1
ENC2005, bay 1  tick2001    ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   524288  2M26360507  1. 10.127.0.78  2.62 Jan 09 2019    I36 v2.72 (03/25/2019)  OV C7000 enclosures 1
ENC2006, bay 1  tick1001    ProLiant BL460c Gen9    Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz   2   524288  2M263604ZZ  1. 10.127.0.58  2.40 Dec 02 2015    I36 v2.20 (05/05/2016)  OV C7000 enclosures 1

Advertisement

Answer

Try below:

Use read().splitlines() as every line comes with its end of line characters (nr) as in your example; this way the characters are removed.

my_List = open('vic_hosts').read().splitlines()

df = pd.read_csv('report.csv')
df = df[df['Server Name'].isin(my_List)]
print(df)

this should be working.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement