Python 将包含类别的列与包含整数的列合并

Python 将包含类别的列与包含整数的列合并,python,pandas,dataframe,Python,Pandas,Dataframe,我想在列之间创建一个关联数据 我的数据当前为以下格式: 我需要将其转换为以下格式: 将T和G中的类别合并到示例列中,这是怎么可能的 谢谢你的帮助 编辑: 打印(df.dtypes)输出: T int64 Group object Sample1 float64 Sample2 int64 Sample3 float64 dtype: object Int64Index([0, 1, 3, 6, 16, 18, 19, ..., 52], dtype='int64') <class

我想在列之间创建一个关联数据

我的数据当前为以下格式:

我需要将其转换为以下格式:

将T和G中的类别合并到示例列中,这是怎么可能的

谢谢你的帮助

编辑:

打印(df.dtypes)
输出:

T
int64
Group
object
Sample1
float64
Sample2
int64
Sample3
float64
dtype: object
Int64Index([0, 1, 3, 6, 16, 18, 19, ..., 52], dtype='int64')
<class 'pandas.core.frame.DataFrame'>
打印(测向索引)
输出:

T
int64
Group
object
Sample1
float64
Sample2
int64
Sample3
float64
dtype: object
Int64Index([0, 1, 3, 6, 16, 18, 19, ..., 52], dtype='int64')
<class 'pandas.core.frame.DataFrame'>
打印(类型(df))
输出:

T
int64
Group
object
Sample1
float64
Sample2
int64
Sample3
float64
dtype: object
Int64Index([0, 1, 3, 6, 16, 18, 19, ..., 52], dtype='int64')
<class 'pandas.core.frame.DataFrame'>


将链接放在数据上会更容易。我不想规定你的数据。请尝试使用PANDAS-Crosstab。

假设原始数据帧名为df,列为T、G和Sample*,以下代码将使用所需格式准备一个新数据帧:

list_T = list(df['T'].unique())
list_G = list(df['G'].unique())
list_Samples = list(df.drop(['T', 'G'], axis = 1).columns)

cols = []
data = []
for s in list_Samples:
    for g in list_G:
        for t in list_T:
            cols.append(s + ' T' + str(t) + ' ' + g)
            data.append(list(df[s][(df['T'] == t) & (df['G'] == g)]))

df2 = pd.DataFrame(data = np.array(data).T, columns = cols)
原始数据帧:

转换数据帧:


你看过了吗?是的,我在考虑使用
df.set_index(['T','Group'])
迭代索引,并为每次迭代创建一个新的数据集和一个新列。有更好的方法吗?当我应用
pd.crosstab(df.Group,df.T,margins=True)
时,我得到错误
TypeError:“int”对象不可编辑
首先检查您拥有的数据类型。print(df.dtypes)和indexend DataFrame:print(type(df))谢谢,我在线程中发布了输出-它说明了什么?