我有一个包含多行和8列的数据文件 - 我想平均第1列,第2列中具有相同数据的第8列 - 例如我的文件可能如下所示:
564645 7371810 0 21642 1530 1 2 30.8007
564645 7371810 0 21642 8250 1 2 0.0103
564645 7371810 0 21643 1530 1 2 19.3619
我想平均第一行和第三行的最后一列,因为列1-2-5是相同的;
我希望输出看起来像这样:
564645 7371810 0 21642 1530 1 2 25.0813
564645 7371810 0 21642 8250 1 2 0.0103
我的文件(文本文件)非常大(~10000行),冗余数据(基于上述规则)不是定期的 - 所以我希望代码找到冗余数据,并将它们平均...
响应larsks评论 - 这是我的4行代码......
import os
import numpy as np
datadirectory = input('path to the data directory, ')
os.chdir( datadirectory)
##READ DATA FILE AND CREATE AN ARRAY
dataset = open(input('dataset_to_be_used, ')).readlines()
data = np.loadtxt(dataset)
##Sort the data based on common X, Y and frequency
datasort = np.lexsort((data[:,0],data[:,1],data[:,4]))
datasorted = data[datasort]
答案 0 :(得分:0)
您可以使用pandas快速完成此操作:
import pandas as pd
from StringIO import StringIO
data = StringIO("""564645 7371810 0 21642 1530 1 2 30.8007
564645 7371810 0 21642 8250 1 2 0.0103
564645 7371810 0 21643 1530 1 2 19.3619
""")
df = pd.read_csv(data, sep="\\s+", header=None)
df.groupby(["X.1","X.2","X.5"])["X.8"].mean()
输出是:
X.1 X.2 X.5
564645 7371810 1530 25.0813
8250 0.0103
Name: X.8
如果您不需要索引,可以致电:
df.groupby(["X.1","X.2","X.5"])["X.8"].mean().reset_index()
这将得到结果:
X.1 X.2 X.5 X.8
0 564645 7371810 1530 25.0813
1 564645 7371810 8250 0.0103
答案 1 :(得分:0)
好的,根据Hury的输入我更新了代码 -
import os #needed system utils
import numpy as np# for array data processing
import pandas as pd #import the pandas module
datadirectory = input('path to the data directory, ')
working = os.environ.get("WORKING_DIRECTORY", datadirectory)
os.chdir( working)
##READ DATA FILE AND and convert it to string
dataset = open(input('dataset_to_be_used, ')).readlines()
data = ''.join(dataset)
df = pd.read_csv(data, sep="\\s+", header=None)
sorted_data = df.groupby(["X.1","X.2","X.5"])["X.8"].mean().reset_index()
tuple_data = [tuple(x) for x in sorted_data.values]
datas = np.asarray(tuple_data)
这适用于测试数据,由hury发布 - 但是当我在df = ...之后使用我的文件似乎不起作用时(我得到的输出如下:
追踪(最近一次通话): 文件“/media/DATA/arxeia/Programming/MyPys/data_refine_average.py”,第31行, df = pd.read_csv(data,sep =“\ s +”,header = None) 在read_csv中输入文件“/usr/lib64/python2.7/site-packages/pandas/io/parsers.py”,第187行 return _read(TextParser,filepath_or_buffer,kwds) 文件“/usr/lib64/python2.7/site-packages/pandas/io/parsers.py”,第141行,在_read f = com._get_handle(filepath_or_buffer,'r',encoding = encoding) 在_get_handle中输入文件“/usr/lib64/python2.7/site-packages/pandas/core/common.py”,第673行 f =开放(路径,模式) IOError:[Errno 36]文件名太长:'564645 \ t7371810 \ t0 \ t21642 \ t1530 \ t1 \ t2 \ t30.8007 \ r \ n564645 \ t7371810 \ t0 \ t21642 \ t8250 \ t1 \ t2 \ t0 \ t0.0103 \ r \ n564645 \ t7371810 \ T0 \ t21642 \ t20370 \ T1 \ T2 \ t0.0042 \ r \ n564645 \ t7371810 \ T0 \ t21642 \ t33030 \ T1 \ T2 \ t0.0026 \ r \ n564645 \ t7371810 \ T0 \ t21642 \ t47970 \ T1 \ T2 \ t0.0018 \ r \ n564645 \ t7371810 \ T0 \ t21642 \ t63090 \ T1 \ T2 \ t0.0013 \ r \ n564645 \ t7371810 \ T0 \ t21642 \ t93090 \ T1 \ T2 \ t0.0009 \ r \ n564645 \ t7371810 \ T0 \ T216 ..........
任何想法?
答案 2 :(得分:0)
这不是最优雅的答案,我不知道它的速度有多快,但我相信它可以根据您提供的信息完成工作:
import numpy
data_file = "full_location_of_data_file"
data_dict = {}
for line in open(data_file):
line = line.rstrip()
columns = line.split()
entry = [columns[0], columns[1], columns[4]]
entry = "-".join(entry)
try: #valid if have already seen combination of 1,2,5
x = data_dict[entry].append(float(columns[7]))
except (KeyError): #KeyError the first time you see a combination of columns 1,2,5
data_dict[entry] = [float(columns[7])]
for entry in data_dict:
value = numpy.mean(data_dict[entry])
output = entry.split("-")
output.append(str(value))
output = "\t".join(output)
print output
我不清楚你是否想要/需要第3列,第6列或第7列,所以我省略了它们。特别是,您没有明确表达如何处理其中可能存在的不同值。如果您可以详细说明您想要的行为(即默认为某个值,或者第一次出现),我建议您填写默认值或将第一个实例存储在词典字典而不是列表字典中。
答案 3 :(得分:0)
import os #needed system utils
import numpy as np# for array data processing
datadirectory = '/media/DATA/arxeia/Dimitris/Testing/12_11'
working = os.environ.get("WORKING_DIRECTORY", datadirectory)
os.chdir( working)
##这里我正在尝试阅读文件,然后在以下行中使用字符串的名称 - 导致下面描述的相同错误(错误#42(我认为) - 名字太大)
data_dict = {} #Create empty dictionary
for line in open('/media/DATA/arxeia/Dimitris/Testing/12_11/1a.dat'): ##above error resolved when used this
line = line.rstrip()
columns = line.split()
entry = [columns[0], columns[1], columns[4]]
entry = "-".join(entry)
try: #valid if have already seen combination of 1,2,5
x = data_dict[entry].append(float(columns[7]))
except (KeyError): #KeyError the first time you see a combination of columns 1,2,5
data_dict[entry] = [float(columns[7])]
for entry in data_dict:
value = np.mean(data_dict[entry])
output = entry.split("-")
output.append(str(value))
output = "\t".join(output)
print output
np.savetxt('sorted_data.dat', sorted, fmt='%s', delimiter='\t') #Save the data