我有大量的平面文件,我需要计算一些指标。 大部分指标都很简单,比如行数和列数很容易实现。 给我的问题是平均列大小。
例如。 这里是一个示例文件
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: float64By the way, your file has an extra '|' at the end of the last line.
更多推荐
发布评论