Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data from one dataframe to another using Pandas transpose?

Objective: to fill in one dataframe with another using transpose

df = pd.DataFrame({'Attributes': ['love', 'family','tech']})
df.T

Produces this output:

               0         1     2
Attributes  love    family  tech

Secondarily, I have another dataframe that is empty:

data = pd.DataFrame(columns = ['Attribute_01',
                'Attribute_02',
                'Attribute_03']

I would like to bring the two dataframes together to produce the following:

Attribute_01  Attribute_02  Attribute_03
love          family        tech
like image 419
Student Avatar asked Dec 13 '25 10:12

Student


1 Answers

Setup

df
  Attributes
0       love
1     family
2       tech

Option 1
rename

df.T.rename(dict(enumerate(data.columns)), axis=1)

           Attribute_01 Attribute_02 Attribute_03
Attributes         love       family         tech

Option 2
set_index

df.set_index(data.columns).T

           Attribute_01 Attribute_02 Attribute_03
Attributes         love       family         tech
like image 171
cs95 Avatar answered Dec 15 '25 23:12

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!