0

I want to transform this dataframe:

    col1    col2
0   [1, 0]  [0, 1]
1   [1, 1]  [1, 1]
2   [0, 1]  [1, 0]
3   [0, 1]  [1, 1]
4   [1, 1]  [0, 1]
5   [1, 0]  [1, 0]

Into this one:

    col1    col2
0   [1, 0]  [0, 1]
1        1       1
2   [0, 1]  [1, 0]
3   [0, 1]       1
4        1  [0, 1]
5   [1, 0]  [1, 0]

I tried to use replace:

df.replace([1,1], 1)

But it didn't work.

2
  • 1
    Looks like an XY problem. Maybe you could tell us how you wind up with a df of lists instead, and why you want to get columns with mixed types (list and ints) in the end? Commented Nov 14, 2018 at 16:29
  • Use tuples instead of lists. Dataframes with lists in behave in weird ways. Commented Nov 14, 2018 at 16:30

1 Answer 1

2

Using applymap

df.applymap(lambda x :  1 if x==[1,1] else x )
Out[162]: 
     col1    col2
0  [1, 0]  [0, 1]
1       1       1
Sign up to request clarification or add additional context in comments.

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.