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?
CodePudding user response:
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]]