【发布时间】:2016-09-02 03:05:51
【问题描述】:
所以。我们有一个 混乱 数据存储在一个 TSV 文件中,我需要对其进行分析。 看起来是这样的
status=200 protocol=http region_name=Podolsk datetime=2016-03-10 15:51:58 user_ip=0.120.81.243 user_agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 user_id=7885299833141807155 user_vhost=tindex.ru method=GET page=/search/
问题是一些行有不同的列顺序/其中一些缺失值,我需要以高性能摆脱它(因为我正在使用的数据集高达 100 GB)。
Data = pd.read_table('data/data.tsv', sep='\t+',header=None,names=['status', 'protocol',\
'region_name', 'datetime',\
'user_ip', 'user_agent',\
'user_id', 'user_vhost',\
'method', 'page'], engine='python')
Clean_Data = (Data.dropna()).reset_index(drop=True)
如您所见,有些列是偏移的。 我做了一个性能非常低的解决方案
ids = Clean_Data.index.tolist()
for column in Clean_Data.columns:
for row, i in zip(Clean_Data[column], ids):
if np.logical_not(str(column) in row):
Clean_Data.drop([i], inplace=True)
ids.remove(i)
所以现在数据看起来不错……至少我可以使用它! 但是我上面提出的方法的高性能替代方案是什么?
unutbu 代码更新:回溯错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-52c9d76f9744> in <module>()
8 df.index.names = ['index', 'num']
9
---> 10 df = df.set_index('field', append=True)
11 df.index = df.index.droplevel(level='num')
12 df = df['value'].unstack(level=1)
/Users/Peter/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in set_index(self, keys, drop, append, inplace, verify_integrity)
2805 if isinstance(self.index, MultiIndex):
2806 for i in range(self.index.nlevels):
-> 2807 arrays.append(self.index.get_level_values(i))
2808 else:
2809 arrays.append(self.index)
/Users/Peter/anaconda/lib/python2.7/site-packages/pandas/indexes/multi.pyc in get_level_values(self, level)
664 values = _simple_new(filled, self.names[num],
665 freq=getattr(unique, 'freq', None),
--> 666 tz=getattr(unique, 'tz', None))
667 return values
668
/Users/Peter/anaconda/lib/python2.7/site-packages/pandas/indexes/range.pyc in _simple_new(cls, start, stop, step, name, dtype, **kwargs)
124 return RangeIndex(start, stop, step, name=name, **kwargs)
125 except TypeError:
--> 126 return Index(start, stop, step, name=name, **kwargs)
127
128 result._start = start
/Users/Peter/anaconda/lib/python2.7/site-packages/pandas/indexes/base.pyc in __new__(cls, data, dtype, copy, name, fastpath, tupleize_cols, **kwargs)
212 if issubclass(data.dtype.type, np.integer):
213 from .numeric import Int64Index
--> 214 return Int64Index(data, copy=copy, dtype=dtype, name=name)
215 elif issubclass(data.dtype.type, np.floating):
216 from .numeric import Float64Index
/Users/Peter/anaconda/lib/python2.7/site-packages/pandas/indexes/numeric.pyc in __new__(cls, data, dtype, copy, name, fastpath, **kwargs)
105 # with a platform int
106 if (dtype is None or
--> 107 not issubclass(np.dtype(dtype).type, np.integer)):
108 dtype = np.int64
109
TypeError: data type "index" not understood
熊猫版本:0.18.0-np110py27_0
更新
一切正常...谢谢大家!
【问题讨论】:
标签: python pandas numpy data-analysis bigdata