如何使用 pandas 转置将数据从一个数据帧添加到另一个数据帧? [英] How to add data from one dataframe to another using Pandas transpose?

查看:68
本文介绍了如何使用 pandas 转置将数据从一个数据帧添加到另一个数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:使用转置用另一个数据填充一个数据框

Objective: to fill in one dataframe with another using transpose

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

产生此输出:

               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

推荐答案

设置

df
  Attributes
0       love
1     family
2       tech

选项1
rename

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

           Attribute_01 Attribute_02 Attribute_03
Attributes         love       family         tech


选项2
set_index


Option 2
set_index

df.set_index(data.columns).T

           Attribute_01 Attribute_02 Attribute_03
Attributes         love       family         tech

这篇关于如何使用 pandas 转置将数据从一个数据帧添加到另一个数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆