2

I have some columns:

0                                            ['XS', '2X']
1                                      ['A2', '2X', '8W']
2                                                  ['2X']
3                    ['A2', 'FR', 'XS', '8W', '8E', '2X']
4                    ['A5', 'FR', 'XS', 'QF', '2X', '23']
5                                      ['XS', '2X', 'MF']
6                                                    None

that I am trying to iterate over. It I loop over the column and get the type:

for item in data1['business_types']:
    print(type(item))

It shows that the types are of type str

<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'NoneType'>

I would like to convert the objects in this column to a list object that I can iterate over.

3
  • You can use the function DataFrame.astype(str) Commented Jun 7, 2017 at 13:03
  • Maybe l = data1['business_types'].tolist() and then l = [ast.literal_eval(i) for i in l] Commented Jun 7, 2017 at 13:04
  • I think your title should be convert column of strings to column of lists of strings Commented Jun 7, 2017 at 13:05

1 Answer 1

2

I think you need ast.literal_eval with fillna for replace None to []:

data1['business_types'] = data1['business_types'].fillna('[]').apply(ast.literal_eval)
print (data1)
             business_types
0                  [XS, 2X]
1              [A2, 2X, 8W]
2                      [2X]
3  [A2, FR, XS, 8W, 8E, 2X]
4  [A5, FR, XS, QF, 2X, 23]
5              [XS, 2X, MF]
6                        []

If need None (NaNs):

def f(x):
    try:
        return ast.literal_eval(x)
    except ValueError:
        return x

import ast

data1['business_types'] = data1['business_types'].apply(f)
print (data1)
             business_types
0                  [XS, 2X]
1              [A2, 2X, 8W]
2                      [2X]
3  [A2, FR, XS, 8W, 8E, 2X]
4  [A5, FR, XS, QF, 2X, 23]
5              [XS, 2X, MF]
6                       NaN

for item in data1['business_types']:
    print(type(item))

<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'float'>
Sign up to request clarification or add additional context in comments.

6 Comments

What version are you using? I actually had tried that and I get ValueError: malformed node or string: None under 0.20.2. I actually just updated from 0.19.2 and it's broke some of my code...
I use pandas 0.20.1, but if bad data, then it can failed :(
i see problem, get my some time
Is possible replace None to empty string?
This worked, I had tried a solution with ast.literal_eval function however, the trick with filling the Nonetypes with [] was clever and worked. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.