0

I have two dataframes df1 and df2. Here df2 is a 2D array table. I need to assign all 2D values of df2 in df1['size_chart'].

Here is my code

total_value_list = [['', 'ウエスト', '股下', '股上', 'ヒップ', '裾回り', 'パンツ丈'], ['XS', '60.5cm', '70.4cm', '24.7cm', '95cm', '35cm', '93.9cm'], ['S', '64.5cm', '71.9cm', '25.7cm', '99cm', '35.5cm', '96.4cm'], ['6XO (7XL)', '100.5cm', '85.4cm', '34.7cm', '135cm', '44.5cm', '118.9cm'], ['M', '68.5cm', '73.4cm', '26.7cm', '103cm', '36.5cm', '98.9cm'], ['L', '72.5cm', '74.9cm', '27.7cm', '107cm', '37.5cm', '101.4cm'], ['O (XL)', '76.5cm', '76.4cm', '28.7cm', '111cm', '38.5cm', '103.9cm'], ['XO (2XL)', '80.5cm', '77.9cm', '29.7cm', '115cm', '39.5cm', '106.4cm'], ['2XO (3XL)', '84.5cm', '79.4cm', '30.7cm', '119cm', '40.5cm', '108.9cm'], ['3XO (4XL)', '88.5cm', '80.9cm', '31.7cm', '123cm', '41.5cm', '111.4cm'], ['4XO (5XL)', '92.5cm', '82.4cm', '32.7cm', '127cm', '42.5cm', '113.9cm'], ['5XO (6XL)', '96.5cm', '83.9cm', '33.7cm', '131cm', '43.5cm', '116.4cm']]

df2 = pd.DataFrame(total_value_list).T

What I have done is -

df1["size_chart"] = df2

This assigns the whole data frame df2 to df1 in dataframe format but not in excel format. I want to write all the 2d array values of df1['size_chart'] in excel format.

After assigning the value of df2 into df1['size_chart'], I am expecting, df1 will look like this -

A B C size_chart D E
1 2 3 V W X Y Z  4 5
      P Q R S T
      L M N O P
      
7
  • 1
    Welcome to SO! please add a MRE. Maybe you should also have a look at how to ask a question Commented Jun 25, 2022 at 11:12
  • also not sure i understand, df2 is a table, how would it look if i put a table inside a column? Commented Jun 25, 2022 at 11:16
  • @OmerBenHaim Yes , df2 is a table. I have to create a nested excel table inside a column. That's where I am stuck. Commented Jun 25, 2022 at 11:21
  • @syedtowfiqurrahim stackoverflow.com/questions/51505504/pandas-nesting-dataframes i think this would help Commented Jun 25, 2022 at 11:23
  • @OmerBenHaim Sorry, the table should be in df1['size_chart'] value and the data of df2 should be in multi-index cells. Again sorry for not demonstrating the problem properly. Commented Jun 25, 2022 at 11:52

1 Answer 1

0

Please try this:

Original data:

total_value_list = [['', 'ウエスト', '股下', '股上', 'ヒップ', '裾回り', 'パンツ丈'], ['XS', '60.5cm', '70.4cm', '24.7cm', '95cm', '35cm', '93.9cm'], ['S', '64.5cm', '71.9cm', '25.7cm', '99cm', '35.5cm', '96.4cm'], ['6XO (7XL)', '100.5cm', '85.4cm', '34.7cm', '135cm', '44.5cm', '118.9cm'], ['M', '68.5cm', '73.4cm', '26.7cm', '103cm', '36.5cm', '98.9cm'], ['L', '72.5cm', '74.9cm', '27.7cm', '107cm', '37.5cm', '101.4cm'], ['O (XL)', '76.5cm', '76.4cm', '28.7cm', '111cm', '38.5cm', '103.9cm'], ['XO (2XL)', '80.5cm', '77.9cm', '29.7cm', '115cm', '39.5cm', '106.4cm'], ['2XO (3XL)', '84.5cm', '79.4cm', '30.7cm', '119cm', '40.5cm', '108.9cm'], ['3XO (4XL)', '88.5cm', '80.9cm', '31.7cm', '123cm', '41.5cm', '111.4cm'], ['4XO (5XL)', '92.5cm', '82.4cm', '32.7cm', '127cm', '42.5cm', '113.9cm'], ['5XO (6XL)', '96.5cm', '83.9cm', '33.7cm', '131cm', '43.5cm', '116.4cm']]

df2 = pd.DataFrame(total_value_list).T

df1 = pd.DataFrame({'A':[1,2,3,4,5,6,7]})

Updating size_cart

df2['combCol'] = df2[[0,1,2,3,4,5,6]].agg(' '.join, axis=1)

df1['size_cart'] = df2['combCol']

print(df1)


  A                                          size_cart
0    1                          XS S 6XO (7XL) M L O (XL)
1    2    ウエスト 60.5cm 64.5cm 100.5cm 68.5cm 72.5cm 76.5cm
2    3       股下 70.4cm 71.9cm 85.4cm 73.4cm 74.9cm 76.4cm
3    4       股上 24.7cm 25.7cm 34.7cm 26.7cm 27.7cm 28.7cm
4    5              ヒップ 95cm 99cm 135cm 103cm 107cm 111cm
5    6        裾回り 35cm 35.5cm 44.5cm 36.5cm 37.5cm 38.5cm
6    7  パンツ丈 93.9cm 96.4cm 118.9cm 98.9cm 101.4cm 103.9cm
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.