1

So I have a pandas data frame similar to this:

col1 col2 col3
[0,1,0] 1 0
[1,0,0] 0 1

and I want to unpack it so that it becomes 5 columns as apparently tensorflow doesn't support nested lists. Is there an efficient way to achieve this?

3
  • Can you provide a formatted example of your data? and also provide an explanation to what you mean with "multiple Tensorflow inputs" Commented Apr 20, 2022 at 5:50
  • 1
    @AloneTogether sorry about the formatting, didn't understand that it needed spaces but it's working now. For what I'm trying to achieve, I'm trying to convert col1 into something like col1-1, col1-2, col-3 etc. as 3 new columns, because TensorFlow doesn't really like there being columns of integers and lists at the same time. Commented Apr 21, 2022 at 11:57
  • @AloneTogether yes, thank you! Commented Apr 23, 2022 at 4:52

1 Answer 1

1

You can try merging the lists with pandas:

import pandas as pd

df = pd.DataFrame(data = {'col1': [[0,1,0], [1,0,0] ], 'col2': [1, 0], 'col3': [0, 1]})

df['col1-1'], df['col1-2'], df['col1-3'] = zip(*list(df['col1'].values))
df = df.drop('col1', axis=1)
print(df)
   col2  col3  col1-1  col1-2  col1-3
0     1     0       0       1       0
1     0     1       1       0       0

Or with numpy:

import pandas as pd
import numpy as np

df = pd.DataFrame(data = {'col1': [[0,1,0], [1,0,0] ], 'col2': [1, 0], 'col3': [0, 1]})

col1 = np.vstack(df['col1'].values)
col23 = df[['col2', 'col3']].values

data = np.concatenate([col1, col23], axis=-1)
print(data)
[[0 1 0 1 0]
 [1 0 0 0 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.