1

I have the following pandas dataframe

data = [{'a': 1, 'b': '[2,3,4,5,6' }, {'a': 10, 'b': '[54,3,40,5'}] 
test = pd.DataFrame(data) 
display(test)

    a     b
0   1   [2,3,4,5,6
1   10  [54,3,40,5

I want to list the number in column b, but as the list has the [ only at the beginning, doesnt allow me to create the list, I'm trying to remove the "[" so I can extract the numbers, but I keep getting errors, what I'm doing wrong?

This is how the numbers are stored

test.iloc[1,1]

'[54,3,40,5'

And this is what I've tried to remove the "[".

test.iloc[0,1].replace("[",'', regex=True).to_list()

test.iloc[0,1].str.replace("[\]\[]", "")

What i want to achieve is to have b as a proper list so i can apply other functions.

    a     b
0   1   [2,3,4,5,6]
1   10  [54,3,40,5]
4
  • When did you get the error, while replacing the character or converting to list? Commented Jul 17, 2020 at 10:21
  • You cannot convert a string to list directly using to_list() Commented Jul 17, 2020 at 10:21
  • try this, pd.eval(test['b'] + "]") or test.b.str.replace(r"[", "").str.split(",") Commented Jul 17, 2020 at 10:28
  • You can try this test['b'] = test['b'].str.findall(r'\d+') Commented Jul 17, 2020 at 11:48

3 Answers 3

1

To make your 'b' column a list you can first delete the open squared bracket at the beginning, and then use the split method on each element of your 'b' column

test['b'] = test['b'].str.replace('[', '').map(lambda x: x.split(','))

test
#     a            b
# 0   1  [2, 3, 4, 5, 6]
# 1  10  [54, 3, 40, 5]
Sign up to request clarification or add additional context in comments.

Comments

0

try it:

def func(col):
  return eval(col+']')

test['b'] = test['b'].apply(func)

2 Comments

This would be a better answer if you explained how the code you provided answers the question.
i add ']' in a string and call function eval() to convert string to object
0
import pandas as pd
data = [{'a': 1, 'b': '[2,3,4,5,6' }, {'a': 10, 'b': '[54,3,40,5'}] 
test = pd.DataFrame(data) 
print(test['b'][0][1:])
for i in range(len(test['b'])):
    test['b'][i] = test['b'][i][1:]

here is the result

Comments

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.