0

I have this pandas DataFrame filled with lists filled with strings, that I wish to split into two frames:

Input:

df = pd.DataFrame({
  'A': {'a': ['NaN'],'b': ['1.11', '0.00']},
  'B': {'a': ['3.33', '0.22'],'b': ['NaN']},
  })

Desired output:

df1 = pd.DataFrame({
  'A': {'a': ['NaN'],'b': ['1.11']},
  'B': {'a': ['3.33'],'b': ['NaN']},
  })
df2 = pd.DataFrame({
  'A': {'a': ['NaN'],'b': ['0.00']},
  'B': {'a': ['0.22'],'b': ['NaN']},
   })

I tried to use the apply function, which works for Series, and was wondering if there is an easy way to apply an operation that achieves this on the entire df.

2
  • Are there always either 1 or 2 items in each list? Commented Mar 21, 2018 at 15:16
  • yes, a mean and a standard deviation, or neither Commented Mar 21, 2018 at 15:20

1 Answer 1

4

You can stack and apply(pd.Series)

s=df.stack().apply(pd.Series)
s[0].unstack()
Out[508]: 
      A     B
a   NaN  3.33
b  1.11   NaN
s[1].unstack()
Out[509]: 
      A     B
a   NaN  0.22
b  0.00   NaN

If you do need object for single cell

s[0].unstack().applymap(lambda x : [x])
Out[512]: 
        A       B
a   [NaN]  [3.33]
b  [1.11]   [NaN]
s[1].unstack().applymap(lambda x : [x])
Out[513]: 
        A       B
a   [nan]  [0.22]
b  [0.00]   [nan]
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.