0

My KBs.csv file:

OS Version,Patch
Server 2019,"['KB4523205', 'KB4519569', 'KB4503308']"

Then I read it:

import pandas as pd
df = pd.read_csv('KBs.csv')
df
OS Version  Patch
0   Server 2019 ['KB4523205', 'KB4519569', 'KB4503308']

The surprise is that when I try to get data from any position in the list, Y get this:

df['Patch'][0][0]
OUT : '['

If I try to build the dateframe manualy:

dfeo = pd.DataFrame({'OS Version': ['Server 2019'],'Patch': [['KB4523205','KB4519569','KB4503308']]})
dfeo
OS Version  Patch
0   Server 2019 [KB4523205, KB4519569, KB4503308]

dfeo['Patch'][0][0]

OUT : 'KB4523205'

This is the behavoir I am looking for, how the csv file must be format to get it?

  • `from ast import literal_eval` and then `df.Patch = df.Patch.apply(literal_eval)` – Trenton McKinney Jul 21 '20 at 17:51
  • `from ast import literal_eval` and then `df = pd.read_csv('KBs.csv', converters={'Patch': literal_eval})` is even better. – Trenton McKinney Jul 21 '20 at 17:54
  • The difference between the two duplicates is, the list values in the dup posted by @Chris, are not in quotes. The list values in the dup I posted do have quotes, so `literal_eval` is a better solution than string slicing and splitting. – Trenton McKinney Jul 21 '20 at 18:08

1 Answers1

0

Thank you very much @Chris, and yes, you were right @Trenton this is was I looking for "from ast import literal_eval"

This is the complete code, it check the minimal patch needed to go live for windows sysop

import pandas as pd
from ast import literal_eval
df = pd.read_csv('KBs.csv', converters={'Patch': literal_eval})

#Minimal Security Patches 2020

w2012 = ['KB4534288', 'KB4537794', 'KB4540694', 'KB4550971', 'KB4556852', 'KB4561674', 'KB4565535']
w2012R2 = ['KB4534309', 'KB4537803', 'KB4541505', 'KB4550970', 'KB4556853', 'KB4561673', 'KB4565540']
w2016 = ['KB4534271', 'KB4537764', 'KB4540670', 'KB4550929', 'KB4556813', 'KB4561616', 'KB4565511']
w2019 = ['KB4561608', 'KB4558998' ]

#The link with column "OS Version"

 d = {'Server 2019': w2019, 'Server 2016': w2016, 'Server 2012 R2': w2012R2, 'Server 2012': w2012}

#The Compare

for k, v in d.items():      
m = df['OS Version'].eq(k)
df.loc[m, 'NoPatch'] =  df.loc[m, 'Patch'].apply(lambda x: list(set(v) - set(x)))
df


Enterprise  Service     Host        OS Version  Patch                    NoPatch
0   Toto    Marketing   consolamig  Server 2019 [KB4523205, KB4519569, KB4503308, KB4499728, K...   [KB4558998, KB4561608]
1   Toto    Marketing   dc01       Server 2016  [KB4525236, KB4485447, KB4520724, KB3192137, K...   [KB4540670, KB4561616, KB4537764, KB4534271, K...
2   Toto    Marketing   duedil01    Server 2012 R2  [KB2920189, KB3103616, KB3045755, KB3161949, K...   [KB4565540, KB4556853, KB4541505, KB4550970, K...
3   Toto    Marketing   win-4ea3    Server 2012 R2  [KB2939471, KB2919355, KB2938772, KB2999226, K...   [KB4565540, KB4556853, KB4541505, KB4550970, K...

Thanks again