我在一个文件夹中有许多文件,如下所示: enter image description here
我正在尝试实现一个数据字典。我对用2个键创建它感兴趣(第一个是http地址,第二个是第三个字段(使用插件),比如adblock)。这些值被引用到不同的度量中,所以我的目的是在字典实现之后,计算每个站点的平均值、中位数和方差。例如,对于平均值,我的意图是考虑文件中所有第4个字段的值,等等。我试图编写此代码,但首先,我不确定它是否正确。 enter image description here
我读了别人的帖子,但没有人解决我的问题,因为他们威胁或只有一个键,或者他们没有显示如何访问字典中的不同值来计算均值,中位数和方差。 问题很简单,承认dictionary实现是可以的,我必须以哪种方式访问key1的不同值:www.google.it->;键2:adblock? 任何形式的帮助都可以接受,我可以提供任何其他答案。你知道吗
你可以用字典做你想做的事,但是你真的应该考虑使用Pandas库。这个库以称为“DataFrame”的表格数据结构为中心,它擅长于按列和按行计算,例如您似乎需要的计算。你知道吗
首先,这里是Pandas代码,它使用read_fwf()方法读取一个文本文件。它还显示第四列的平均值和方差:
数据帧对象中有different ways of selecting columns and rows。前面示例中的方括号
[ ]
按列号选择了数据帧中的一列。如果只想从第三列中包含adblock
的行中计算第四列的平均值,可以这样做:编辑: 您还可以同时计算多个列的平均值或方差:
结束编辑
如果要从多个文件中读取数据并对连接的数据进行计算,可以使用concat()方法。此方法获取DataFrame对象的列表并连接它们(默认情况下,按行)。使用以下行从目录中的所有
*.txt
文件创建数据帧:相关问题 更多 >
编程相关推荐