gpt4 book ai didi

python - 根据另一个表制作多个表?

转载 作者:行者123 更新时间:2023-11-28 17:26:53 25 4
gpt4 key购买 nike

我有许多格式为“NameX(TypeY)”的列。格式是这样的:

total =  pd.DataFrame(table):

NameA(Type1) NameA(Type2) NameA(Type3) NameB(Type1) NameB(Type2) NameB.(Type3)
set1 1 1 1 2 3 4
set2 2 3 1 1 0 2
set3 2 2 1 0 0 3

所以我现在的目标是以这种格式为每个名称(A、B、X...)获取一个表,我不想更改设置行,因此名称 A 的示例输出将是:

     Type1       Type2       Type3
set1 1 1 1
set2 2 3 1
set3 2 2 1

我正在考虑以某种方式使用 for 循环,但无法完全弄清楚如何获得多个这样的表。

任何帮助将不胜感激!我对编程和 Python 还很陌生,所以仍在研究一些更基本的原理

最佳答案

试试这个:

对于一个:

df2 = df.filter(like='NameA', axis=1).rename(columns=lambda x: x.replace('NameA', '').replace('(', '').replace(')', ''))

对于列表:

Dflist = ['NameA','NameB' ] 
DfDict = {}

for e in Dflist:
out = df.filter(like=e, axis=1).rename(columns=lambda x: x.replace(e, '').replace('(', '').replace(')', ''))
# print out , "\n"
DfDict[e] = out

DfDict["NameA"]

# Type1 Type2 Type3
# set1 1 1 1
# set2 2 3 1
# set3 2 2 1

关于python - 根据另一个表制作多个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38166313/

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