0

The list that I want to convert into a dataframe stores different data types in each cell:

[In]  type(example_list)
[Out] list

[In]  type(example_list[0])
[Out] list

[In]  type(example_list[0][0])
[Out] str

[In]  type(example_list[0][1])
[Out] tuple

[In]  type(example_list[0][2])
[Out] tuple

[In]  type(example_list[0][1][0])
[Out] list

And the tuple elements all have the following format:

[In]  example_list[0][1]
[Out] ([array([200.85183333, 200.85183333, 200.85183333])], ['#c8c8c8'])

[In] type(example_list[0][1][0])
[Out] list

[In] type(example_list[0][1][1])
[Out] list

When I use the pd.DataFrame function I end up with 3 columns, but I want to have 5 columns (that is, 2 columns from each tuple element).

When I run the following command:

df = pd.DataFrame(example_list, columns=['Name','PrimaryColors','SecondaryColors'])
df.to_csv('test.csv', sep=',')

This is what I get (outputting only the first row below):

E123    ([array([200.85183333, 200.85183333, 200.85183333])], ['#c8c8c8'])  ([array([226.9, 226.9, 226.9])], ['#e2e2e2'])

How can I instead end up with 5 columns instead of 3 and have the dataframe follow the following format?

Name    PrimaryColorRGB                             PrHEX       SecondaryColorsRGB       SecHEX
E123    200.85183333, 200.85183333, 200.85183333    #c8c8c8     226.9, 226.9, 226.9      #e2e2e2
1
  • 1
    1. Sorry, but this way the structure of your input list is not clear to me. 2. Where does this input originate from - could you adjust or change it to be processed better into a dataframe? Commented Jan 31, 2020 at 9:49

1 Answer 1

1

Iiuc, you can create your desired result from your 3-column-dataframe according to jezrael's answer to a similar question here: https://stackoverflow.com/a/35491399/8300135

In your case:

df[['PrimaryColorsRGB','PrimaryColorsHex']] = pd.DataFrame(df.PrimaryColors.values.tolist(), index= df.index)

The same with your secondary colors column.

However, in your case you still get column entries which are lists with only one element, a tuple in one column and a string in the other.

You could address this issue like

df.PrimaryColorsRGB = df.PrimaryColorsRGB.str[0]
df.PrimaryColorsHex = df.PrimaryColorsHex.str[0]

Again, the same with secondary colors columns.

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.