1

I am a beginner in Data Science and I am trying to pivot this data frame using Pandas:

enter image description here

So it becomes something like this: (The labels should become the column and file paths the rows.)

enter image description here

I tried this code which gave me an error:

enter image description here

EDIT:

I have tried Marcel's suggestion, the output it gave is this:enter image description here

The "label" column is a group or class of file paths. I want to convert it in such a way it fits this function: tf.Keras.preprocessing.image.flow_from_dataframe in categorical

Thanks in advance to all for helping me out.

plutolaser
  • 448
  • 4
  • 17

3 Answers3

3

I did not understand your question very well, but if you just want to convert columns to rows then you can do

train_df.T

which means transpose

user16714199
  • 228
  • 6
  • 19
  • Hi, thanks for your answer. I want something like this https://stackoverflow.com/questions/17298313/python-pandas-convert-rows-as-column-headers – plutolaser Sep 26 '21 at 16:03
1

I think you are looking for something like this:


import pandas as pd

df = pd.DataFrame({
    'labels': ['a', 'a', 'a', 'b', 'b'],
    'pathes' : [1, 2, 3, 4, 5]
})

labels = df['labels'].unique()
new_cols = []
for label in labels:
    new_cols.append(df['pathes'].where(df['labels'] == label).dropna().reset_index(drop=True))
df_final = pd.concat(new_cols, axis=1)

print(df_final)

marcel h
  • 742
  • 1
  • 7
  • 20
0

I've found what was wrong, I misunderstood y_col and x_col in tf.Keras.preprocessing.image.ImageDataGenerator.flow_from_dataframe. Thanks to all of you for your contributions. Your answers are all correct in different ways. Thanks again Marcel h and user16714199!

plutolaser
  • 448
  • 4
  • 17