Home > OS >  I am getting unwanted tuples in my dataframe, how to stop this
I am getting unwanted tuples in my dataframe, how to stop this

Time:12-16

First post here. I'm at my wits end!

Pandas is making tuples in my dataframe when I do the below piece of code.

Essentially, I wanted to normalize all my columns except two. So I .pop them and then after normalization, when I try to put them back in, it stores them as a tuples, even though they are clearly lists.

class_ = dataset.pop("class")
path_ = dataset.pop("path")

layer = tf.keras.layers.LayerNormalization(axis=0)
g = layer(dataset)
gh = pd.DataFrame(np.array(g),columns=[dataset.columns.values])
gho = gh.sort_index(axis=1)
gho["path"] = list(path_)
gho["class"] = list(class_)

I expect something like this when I print

print(gho.columns.values)
['path' 'class' 'velocity']

But I keep getting this

[('path',) ('class',) ('velocity',)]

CodePudding user response:

You likely have a MultiIndex with a single level, use:

df.columns.get_level_values(0).tolist() # or to_numpy() / values

Output: ['path', 'class', 'velocity']

CodePudding user response:

You can remove [] for avoid one level MultiIndex:

gh = pd.DataFrame(np.array(g),columns=[dataset.columns.values])

convert to:

gh = pd.DataFrame(np.array(g),columns=dataset.columns)
  • Related