Skip to content Skip to sidebar Skip to footer

Pandas To Read Csv File And Grep The Hostnames From A File

I have a CSV file which i'm trying to read with pandas based on the below code. Code snippet: import pandas as pd import os data = pd.read_csv(input('Please input the CSV File Name

Solution 1:

I think you after you create df you need to read in host_list.txt to another dataframe.

df2 = pd.read_csv('host_list.txt',header=None)
df2.columns = ['host_list']


df2
Out[13]: 
  host_list
0    host01
1    host02
2    host03
3    host04
4    host05
5    host06
6    host07
7    host08
8    host09

If you then want to subset df based on the hosts in common with df2, you could use pandas.DataFrame.isin

df = df[df['Target system address'].isin(df2.host_list.unique())]

Which returns:

  Target system address                       Safe
0                host01  TDS-PAR-DEFAULT-UNIX-ROOT
1                host06       TDS-OT-SCM-UNIX-ROOT
2                host09  TDS-PAR-DEFAULT-UNIX-ROOT

Post a Comment for "Pandas To Read Csv File And Grep The Hostnames From A File"