I was recently trying to read in a csv, of which one of the columns is serialized as list strings. Using ast.literal_eval usually converts the string to an actual list. In this case, even though we check whether a is a list or not, we get back an error saying None type is non subscriptable.
Code:
def generate(name):
df = pd.read_csv(name)
df['column'] = df['column'].apply(lambda x: literal_eval((x))) # all x should become list
for a in df['column']:
if a and isinstance(a, list):
print(a)[0]
else:
print('jey')
Error message (the first list is printed)
[0.0, 0.0, 0.014, 0.0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-34-d84f295beae6> in <module>
----> 1 generate('parameter_vv/perimeter_log.csv')
<ipython-input-33-e9ee10a439d4> in generate(name)
4 for a in df['column']:
5 if a and isinstance(a, list):
----> 6 print(a)[0]
7 else:
8 print('jey')
TypeError: 'NoneType' object is not subscriptable
printreturnsNone. I think you wantedprint(a[0])