平均列大小使用熊猫(average column size using pandas)

编程入门 行业动态 更新时间:2024-10-09 15:23:39
平均列大小使用熊猫(average column size using pandas)

我有大量的平面文件,我需要计算一些指标。 大部分指标都很简单,比如行数和列数很容易实现。 给我的问题是平均列大小。

例如。 这里是一个示例文件

header1|header2|header3|header4|header5 this|is|1|12-Dec-1995|0xFF1 is||57|14-Jan-2014||

如何计算文件的平均列大小。 当我执行str.len()时,遇到非字符串列时会出现错误。

谢谢你的帮助

I have huge flat files for which I need to compute some metrics. Most of the metrics are simple like row count and column count and easily accomplished. The one that is giving me issues is average column size.

For eg. here is a sample file

header1|header2|header3|header4|header5 this|is|1|12-Dec-1995|0xFF1 is||57|14-Jan-2014||

How do I calculate average column size for the file. When I do str.len() it gives me an error when it encounters non-string column.

Thanks for your help

最满意答案

如果'列大小'表示'列宽',那么这应该工作:

df.fillna('').astype(str).apply(lambda x:x.str.len()).mean() #header1 3.0 #header2 1.0 #header3 1.5 #header4 11.0 #header5 2.5 #dtype: float64

顺便说一句,你的文件有一个额外的'|' 在最后一行的末尾。

If by 'column size' you mean 'column width', then this should work:

df.fillna('').astype(str).apply(lambda x:x.str.len()).mean() #header1 3.0 #header2 1.0 #header3 1.5 #header4 11.0 #header5 2.5 #dtype: float64

By the way, your file has an extra '|' at the end of the last line.

更多推荐

本文发布于:2023-04-29 08:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:熊猫   平均   大小   average   size

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!