gpt4 book ai didi

python - 在每个组上方添加每个组的名称

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:58 25 4
gpt4 key购买 nike

我有一个 excel 文件,其中样本按行列出,属性按列列出。我正在使用 python 按一个或多个属性对示例进行分组,并使用 xlsxwriter 进行导出。有没有办法在组上方的行中自动插入每个组的标题?

type1 = ['a', 'c', 'e']
t1 = result.loc[df['Name'].isin(type1)]

type2 = ['b','f']
t2 = result.loc[df['Name'].isin(type2)]

group_by_type = [t1, t2]

编辑

      Attribute1         Attribute2          Attribute3  Calculation1
0 1.565417 102.975496 a -0.348246
1 1.577817 129.019182 b -0.984943
2 1.577817 145.013103 c -0.624412
3 1.594367 158.924852 d -0.701562
4 1.586100 159.029720 e -0.436547
5 1.594367 160.920870 f -0.664863
6 1.586100 161.045205 g 0.856694
7 1.598500 204.981242 h -0.547259
8 1.619200 173.009131 i -0.583041
9 1.623333 175.024768 j -0.640267

type1 = ['a', 'c', 'e']
t1 = result.loc[df['Attribute3'].isin(type1)]

type2 = ['b','f']
t2 = result.loc[df['Attribute3'].isin(type2)]

titles = ['title1', 'title2']

result2 = pd.concat([t1, t2], keys=titles, axis=0)

print(result2[0:10])

Attribute1 Attribute2 Attribute3 Calculation1
title1 0 1.565417 102.975496 a -0.348246
2 1.577817 145.013103 c -0.624412
4 1.586100 159.029720 e -0.436547
title2 1 1.577817 129.019182 b -0.984943
5 1.594367 160.920870 f -0.664863

最佳答案

您可以将 concatkeys 参数一起使用:

np.random.seed(0)
df1 = pd.DataFrame(np.random.choice(10, (3, 3)), columns=list('ABC'))
df2 = pd.DataFrame(np.random.choice(10, (3, 3)), columns=list('DEF'))

titles = ['first', 'second']
pd.concat([df1, df2], keys=titles, axis=1)

first second
A B C D E F
0 5 0 3 4 7 6
1 3 7 9 8 8 1
2 3 5 2 6 7 7

关于python - 在每个组上方添加每个组的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53799679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com