在Python中生成数据透视表

吉姆·雷诺(Jim Raynor)

假设我有100个文件,并遍历所有文件。在每个文件中,都有几个属性的记录:(在读取所有文件之前,未知属性的总数)

假设有一个简单的情况,在读取所有文件后,我们获得20个不同的属性和以下信息:

File_001: a1, a3, a5, a2
File_002: a1, a3
File_003: a4
File_004: a4, a2, a6
File_005: a7, a8, a9
...
File_100: a19, a20

[更新]或以另一种表示形式,其中每一行是一个文件和一个属性之间的单个匹配项:

File_001: a1
File_001: a3
File_001: a5
File_001: a2
File_002: a1
File_002: a3
File_003: a4
File_004: a4
File_004: a2
File_004: a6
...
File_100: a19
File_100: a20

如何生成“反向”统计表,即:

a1: File_001, File_002, File_006, File_083
a2: File_001, File_004
...
a20: File_099, File_100

如何在Python(2.7.x)中做到这一点?(有或没有熊猫。我认为熊猫可能会有所帮助)

最大容量

UPDATE2: 如何生成“反向”统计表

In [9]: df
Out[9]:
        file attr
0   File_001   a1
1   File_001   a3
2   File_001   a5
3   File_001   a2
4   File_002   a1
5   File_002   a3
6   File_003   a4
7   File_004   a4
8   File_004   a2
9   File_004   a6
10  File_100  a19
11  File_100  a20

In [10]: df.groupby('attr')['file'].apply(list)
Out[10]:
attr
a1     [File_001, File_002]
a19              [File_100]
a2     [File_001, File_004]
a20              [File_100]
a3     [File_001, File_002]
a4     [File_003, File_004]
a5               [File_001]
a6               [File_004]
Name: file, dtype: object

更新:

如何将output [202]设置为DataFrame?

new = (df.set_index('file')
         .apply(lambda x: pd.Series(x['attr']), axis=1)
         .stack()
         .reset_index(level=1, drop=True)
         .reset_index(name='attr')
         .groupby('attr')['file']
         .apply(list)
)

所以我可以将其导出到html或csv?

new.to_csv('/path/to/file.csv', index=False)

或者

html_text = new.to_html(index=False)

原始答案:

这是一个熊猫解决方案:

原始DF:

In [201]: df
Out[201]:
       file              attr
0  File_001  [a1, a3, a5, a2]
1  File_002          [a1, a3]
2  File_003              [a4]
3  File_004      [a4, a2, a6]
4  File_005      [a7, a8, a9]
5  File_100        [a19, a20]

解决方案:

In [202]: %paste
(df.set_index('file')
   .apply(lambda x: pd.Series(x['attr']), axis=1)
   .stack()
   .reset_index(level=1, drop=True)
   .reset_index(name='attr')
   .groupby('attr')['file']
   .apply(list)
)
## -- End pasted text --

输出:

Out[202]:
attr
a1     [File_001, File_002]
a19              [File_100]
a2     [File_001, File_004]
a20              [File_100]
a3     [File_001, File_002]
a4     [File_003, File_004]
a5               [File_001]
a6               [File_004]
a7               [File_005]
a8               [File_005]
a9               [File_005]
Name: file, dtype: object

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Python中生成数据透视表

来自分类Dev

如何使用LINQ生成数据透视表?

来自分类Dev

VBA - 生成数据透视表时对象需要错误

来自分类Dev

生成数据透视表时出现错误 438

来自分类Dev

在Hive中,如何从表中生成数组类型数据

来自分类Dev

如何从 Hyperledger 结构块生成数据透视表数据(汇总数据)

来自分类Dev

如何在python中生成指定范围内的合成数据?

来自分类Dev

生成数据透视表时如何在记录的宏(VBA)中使用动态源数据

来自分类Dev

RDLC报告,生成数据透视图

来自分类Dev

如何从单独的变量列表中生成数据框

来自分类Dev

数据透视表生成错误

来自分类Dev

在表中生成相同的数据

来自分类Dev

使用GridView生成数据表

来自分类Dev

从 tcpdf 表中的 db 生成数据

来自分类Dev

如何在python中生成数字的随机序列?

来自分类Dev

在python中生成数组列的所有可能组合

来自分类Dev

在SQL中生成表达式字符串以在数据透视表中添加列的值

来自分类Dev

使用python的数据透视表

来自分类Dev

遍历python数据透视表

来自分类Dev

如何从循环的 n 次迭代生成的 n 个字典列表中生成数据帧?

来自分类Dev

在数据表中生成标记

来自分类Dev

如何在文本框中生成数据在gridline Grid Kendo UI中创建数据

来自分类Dev

使用日历表生成数据的历史视图

来自分类Dev

使用 Hibernate 从类生成数据库表

来自分类Dev

如何更新/生成数据库表模型?

来自分类Dev

在Ruby中生成数字序列

来自分类Dev

在 Foreach 元素中生成数组键

来自分类Dev

我可以使用数据透视表/切片器从大型 Excel 工作表中生成包含详细信息的表格列表吗?

来自分类Dev

如何使用PIVOT在SQL查询中生成数据驱动的列

Related 相关文章

热门标签

归档