【发布时间】:2023-04-08 18:55:02
【问题描述】:
我正在尝试实现一个表的this python solution to count the number of lines with identical content in the first few columns。这是我的代码:
#count occurrences of reads
import pandas as pd
#pd.options.display.large_repr = 'info'
#pd.set_option('display.max_rows', 100000000)
#pd.set_option('display.width',50000)
import sys
file1 = sys.argv[1]
file2 = file1[:4] + '_multi_nobidir_count.soap'
df = pd.read_csv(file1,sep='\t',header=None)
df.columns = ['v0','v1','v2','v3','v4','v5','v6','v7','v8','v9','v10','v11']
df['v3']=df.groupby(['v0','v1','v2']).transform(sum).v3
df.to_csv(file2,sep='\t',index=False,header=False)
它适用于测试数据(200 行),但当我将其应用于真实数据(2000 万行)时出现以下错误:
Traceback (most recent call last):
File "count_same_reads.py", line 14, in <module>
df['v3']=df.groupby(['v0','v1','v2']).transform(sum).v3
File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0-py2.7-linux-x86_64.egg/pandas/core/groupby.py", line 2732, in transform
return self._transform_item_by_item(obj, fast_path)
File "/usr/local/lib/python2.7/dist-packages/pandas-0.14.0-py2.7-linux-x86_64.egg/pandas/core/groupby.py", line 2799, in _transform_item_by_item
raise TypeError('Transform function invalid for data types')
TypeError: Transform function invalid for data types
如何进行故障排除,找出我收到此错误的原因?
[编辑] 取消注释 pd.options. 和 pd.set_option 行并没有改变结果。
[EDIT2] 考虑到下面的一些回复,我在我的数据上运行了以下代码以输出第 4 列中没有数字的任何数据行:
#test data type
import sys
file1 = sys.argv[1]
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
with open(file1, 'r') as data:
for row in data:
a = row.strip().split()[3]
if is_number(a) == False:
print row.strip()
这适用于测试数据,其中我将行的第四列值之一从1 更改为e,它只输出包含字母而不是数字的行。我在原始大数据上运行它,但没有返回任何行。
【问题讨论】:
-
也许只转换
v3列有效:df.v3 = df.groupby(['v0','v1','v2']).v3.transform(sum)
标签: python debugging pandas bigdata