【发布时间】:2018-10-15 02:53:32
【问题描述】:
我正在使用 Python 中的 Pandas 读取文本文件。我正在使用 Python 2.7。这个问题中使用的数据集与我在here 之前提出的一个问题有关。具体来说,我的数据的前两行和第一列由文本信息组成。以下是我的数据集的截断版本的快照。
数据文件可以在here找到。我正在使用here 给出的有用答案来加载数据集 (df = pd.read_csv('dum.txt',sep='\t', header=[0,1], index_col=0))。
我想沿行而不是列获取我的 pandas 数据框的描述性统计信息。我曾尝试使用df.describe(),但它为我提供了沿列的描述性统计信息。我查看了this 问题中给出的答案,但是当我使用该链接中建议的答案时出现以下错误。
TypeError: ('unbound method describe() must be called with DataFrame instance as first argument (got Series instance instead)', u'occurred at index foxq1')
如何使用 Pandas 为我拥有的数据集的每一行中的数字条目获取描述性统计信息?提前致谢。
在几个 cmets 之后,我将包括我正在使用的实际代码和错误消息:
实际代码是这样的:
df = pd.read_csv('dum.txt',sep='\t', header=[0,1], index_col=0)
df.apply(pd.DataFrame.describe, axis=1)
错误信息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-0d7a5fde0f42> in <module>()
----> 1 df.apply(pd.DataFrame.describe, axis=1)
2 #df.apply(pd.DataFrame.describe, axis=1)
/Users/LG/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4260 f, axis,
4261 reduce=reduce,
-> 4262 ignore_failures=ignore_failures)
4263 else:
4264 return self._apply_broadcast(f, axis)
/Users/LG/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce)
4356 try:
4357 for i, v in enumerate(series_gen):
-> 4358 results[i] = func(v)
4359 keys.append(v.name)
4360 except Exception as e:
TypeError: ('unbound method describe() must be called with DataFrame instance as first argument (got Series instance instead)', u'occurred at index object1')
【问题讨论】:
-
请附上导致问题的实际代码和完整的错误信息。
-
@DYZ:我现在包含了代码和完整的错误消息。希望对您有所帮助。
-
@DYZ:我使用的是 Python 2.7。我想知道这是否可能是我收到错误的原因。
-
您包含的代码(两次!)肯定不是实际代码,因为它没有导致错误的行。
-
@DYZ:抱歉,现在更正了。
标签: python python-2.7 pandas