3

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

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

I think you just need to change the column name in df1

df.columns=data.columns
df
Out[741]: 
           Attribute_01 Attribute_02 Attribute_03
Attributes         love       family         tech

Comments

0

Use .loc accessor to set the first row of data using a listified df['Attributes'].

data.loc[0] = df['Attributes'].tolist()

Result:

  Attribute_01 Attribute_02 Attribute_03
0         love       family         tech

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.