【问题标题】:Descriptive statistics along rows of text file using Pandas使用 Pandas 沿文本文件行的描述性统计
【发布时间】: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


【解决方案1】:

从您引用的the question,您可以只使用此代码(换句话说,沿行应用描述):

df.apply(pd.DataFrame.describe, axis=1)

你会得到以下结果:

         count  mean       std  min  25%  50%  75%  max
object1    5.0   3.1  1.581139  1.1  2.1  3.1  4.1  5.1
object2    5.0   3.2  1.581139  1.2  2.2  3.2  4.2  5.2
object3    5.0   3.3  1.581139  1.3  2.3  3.3  4.3  5.3
object4    5.0   3.4  1.581139  1.4  2.4  3.4  4.4  5.4
object5    5.0   3.5  1.581139  1.5  2.5  3.5  4.5  5.5
object6    5.0   3.6  1.581139  1.6  2.6  3.6  4.6  5.6
object7    5.0   3.7  1.581139  1.7  2.7  3.7  4.7  5.7
object8    5.0   3.8  1.581139  1.8  2.8  3.8  4.8  5.8

【讨论】:

  • 使用您描述的方法时出现以下错误:TypeError: ('unbound method describe() must be called with DataFrame instance as first argument (got Series instance instead)', u'occurred at index object1')
  • Joe:你使用的是 Python 2.7 吗?
  • 抱歉,我使用的是 Python 3.6。这可能是问题所在。还有你用的是什么版本的熊猫?
  • Joe:非常感谢您的帮助,非常感谢。 =) 我使用的熊猫版本是这样的:`u'0.22.0'`
  • 几个月前我从 0.21.x 更新,因为命令无法正常工作。我现在使用的是 0.23.4。您可以尝试更新 pandas,看看是否能解决问题。
【解决方案2】:

您可以尝试使用 numpy 来获取行的大部分统计信息:

df = pd.read_csv('dum.txt',sep='\t', header=[0,1], index_col=0)
print df 

Type      T1   T2   T3   T4   T5   T6   T7
Tag     Tag1 Tag1 Tag1 Tag5 Tag5 Tag6 Tag6
object1  1.1  2.1  3.1  4.1  5.1  6.1  7.1
object2  1.2  2.2  3.2  4.2  5.2  6.2  7.2
object3  1.3  2.3  3.3  4.3  5.3  6.3  7.3
object4  1.4  2.4  3.4  4.4  5.4  6.4  7.4
object5  1.5  2.5  3.5  4.5  5.5  6.5  7.5
object6  1.6  2.6  3.6  4.6  5.6  6.6  7.6
object7  1.7  2.7  3.7  4.7  5.7  6.7  7.7
object8  1.8  2.8  3.8  4.8  5.8  6.8  7.8

data = df.values
data_mean = np.mean(data, axis=1)
data_std = np.std(data, axis=1)
data_min = np.min(data, axis=1)
data_max = np.max(data, axis=1)

print data_mean 

[ 4.1  4.2  4.3  4.4  4.5  4.6  4.7  4.8]

print data_std

[ 2.  2.  2.  2.  2.  2.  2.  2.]

print data_min

[ 1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8]

print data_max

[ 7.1  7.2  7.3  7.4  7.5  7.6  7.7  7.8]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多