Reputation: 99
I have a dataframe column NVD where each row contains a list. What I did was create a few new columns and I'd like to add the first value from the list to a specific column, the second value from the list to another etc. However I'm running into this error IndexError: list index out of range
Here's my starting DataFrame:
poam = pd.read_excel('NetApp-RET-NVD_V1.xlsx', header = 0)
new_poam = poam[["POAM ID ", 'NVD']]
# Split string into separate values and create list from values
new_poam['NVD'] = new_poam['NVD'].astype(str)
new_poam['NVD'] = [x[10:] for x in new_poam['NVD']]
new_poam['NVD'] = [x.replace("\/", ",") for x in new_poam['NVD']]
new_poam['NVD'] = [x.split(",") for x in new_poam['NVD']]
print(new_poam)
>>> Output
POAM ID NVD
0 V-1744 [AV:N, AC:L, PR:H, UI:N, S:U, C:N, I:N, A:H]
1 V-1747 [AV:N, AC:L, PR:H, UI:N, S:U, C:N, I:N, A:H]
2 V-1748 [AV:N, AC:L, PR:H, UI:N, S:U, C:N, I:N, A:H]
3 V-1749 [AV:N, AC:L, PR:H, UI:N, S:U, C:N, I:N, A:H]
4 V-1751 [AV:N, AC:L, PR:L, UI:N, S:U, C:N, I:N, A:H]
.. ... ...
200 V-2080 []
201 V-2082 []
202 V-2083 []
203 V-2084 []
204 V-2085 []
Column NVD is a list of values like I wanted- so now I wanted to add all of my new columns and add values from the lists to those columns, here's how I accomplished that:
new_poam.reindex(columns = new_poam.columns.tolist() + ['Attack Vector','Attack Complexity', 'Privileges Required', 'User Interaction', 'Impact Metrics: Confidentiality', 'Impact Metrics: Integrity', 'Impact Metrics: Availability', 'Remediation Level', 'List of Risk Reeduction'])
new_poam['Attack Vector'] = [x[0] for x in new_poam['NVD']]
print(new_poam)
>>> Output
POAM ID NVD Attack Vector
0 V-1744 [AV:N, AC:L, PR:H, UI:N, S:U, C:N, I:N, A:H] AV:N
1 V-1747 [AV:N, AC:L, PR:H, UI:N, S:U, C:N, I:N, A:H] AV:N
2 V-1748 [AV:N, AC:L, PR:H, UI:N, S:U, C:N, I:N, A:H] AV:N
3 V-1749 [AV:N, AC:L, PR:H, UI:N, S:U, C:N, I:N, A:H] AV:N
4 V-1751 [AV:N, AC:L, PR:L, UI:N, S:U, C:N, I:N, A:H] AV:N
.. ... ... ...
200 V-2080 []
201 V-2082 []
202 V-2083 []
203 V-2084 []
204 V-2085 []
So you can see that the first value from my lists got added to the Attack Vector column, but now when I try adding the second value from the lists to the Attack Complexity column like so:
new_poam['Attack Complexity'] = [x[1] for x in new_poam['NVD']]
I get the error mentioned earlier IndexError: list index out of range
I'm assuming this happens because some of the rows for the NVD column have no values, so it's an empty list. When I add the [0]
value from the list, it just does nothing with the empty lists, but then when I add any other values, I think that's what's causing the error? If that's the case, how do I make it just add a generic "Null" value to each of the new columns, if it's an empty list?
Upvotes: 0
Views: 453
Reputation: 1628
In this case, just make an if statement
l = []
for i in new_poam['NVD']:
if not i:
l.append('0')
else:
try:
l.append(i[1])
except:
l.append('0')
df['new_column'] = l
The condition if not
will check if your list is empty
Upvotes: 1