How do I extract and display the sum of string values from a specific column of csv file separately?

28 views Asked by At

I am planning to write a batch script where in I need to scan the values from a particular column of a CSV file one by one and store calculate sum of them.

Say, following is the column in CSV file:

['CVE-2023-34454(H)', 'CVE-2022-4450(H)', 'CVE-2023-0401(H)', 'CVE-2023-2976(H)', 
'CVE-2023-46589(C)', 'CVE-2023-0217(H)', 'CVE-2022-48174(C)', 'CVE-2022-3996(H)', 
'CVE-2023-34455(H)', 'CVE-2016-7798(H)', 'CVE-2023-2004(H)', 'CVE-2023-44487(C)']

I have error when i run my code :

import pandas as pd

df = pd.read_csv('scan.csv')

# Filter lines ending with (C)
c_rows = df[df['vulnerability'].str.endswith('(C)')]

# Count occurrences of each value
c_counts = c_rows['vulnerability'].value_counts()

# Filter lines ending with (H) 
h_rows = df[df['vulnerability'].str.endswith('(H)')]

# Count occurrences of each value
h_counts = h_rows['vulnerability'].value_counts()

print("Occurrences of values ending with (C):")
print(c_counts)

print("Occurrences of values ending with (H):") 
print(h_counts)

Error :

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
File c:\Users\hello\OneDrive\Documents\python\scan_pd.py:6
      3 df = pd.read_csv('scan_images.csv')
      5 # Filtrer les lignes se terminant par (C)
----> 6 c_rows = df[df['vulnerability'].str.endswith('(C)')]
      8 # Compter les occurrences de chaque valeur 
...
   3815     #  InvalidIndexError. Otherwise we fall through and re-raise
   3816     #  the TypeError.
   3817     self._check_indexing_error(key)

KeyError: 'vulnerability'
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings

do you have any idea about this error, thanks in advance.

0

There are 0 answers