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

标签 python python-3.x pandas filter

我有许多格式为“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/

相关文章:

python pandas系列获取一行的索引号

python - 从 lambda 分配给 Python 列表的一部分

python-3.x - GCP - 获取有关存储桶的完整信息

Python - <__main__.MyClass 对象在 0x0000000002B70E10>

python - 按列表排序索引 - Python Pandas

python - Matplotlib 工作流程

python - 如何将原始字节数组作为二进制文件写入谷歌云存储

python - 在 Python 中访问 2D 列表/数组并进行计算的更快方法?

python - Scapy sniff() 似乎没有捕获 TCP 数据包,只显示以太网帧

python-3.x - 如何同时监听D-Bus事件和IPC channel ?