【发布时间】:2014-12-03 16:03:19
【问题描述】:
我正在尝试解析一些数据以生成直方图
数据在多列中,但对我来说唯一相关的列是下面的两列。
X
AB 42
CD 77
AB 33
AB 42
AB 33
CD 54
AB 33
仅对于带有 AB 的行,我想绘制 col 2 中值的直方图。所以直方图应该排序和绘制:
33 - 3
42 - 2
(虽然 42 先出现,但我想先绘制 33)。
我有很多列,但它需要 grep 'AB' 字符并且只在这些行中搜索。有人可以帮忙吗?
更新:数据在一个 csv 文件中,并且有几列。
编辑:我现在有这种格式的 csv 文件中的数据。
地址、数据
来自AP,42
来自AP,42
来自AP,33
ToAP,77
来自AP,54
来自AP,42
来自AP,42
来自AP,33
ToAP,42
来自AP,42
来自AP,33
如果我使用来自@dranxo 的代码,
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv', sep=',')
df_useful = df[df['Addresses'] == 'FromAP']
df_useful.hist()
plt.show()
我收到以下错误:
Laptop@ubuntu:~/temp$ ./a.py
/usr/lib/pymodules/python2.7/matplotlib/axes.py:8261: UserWarning: 2D hist input should be nsamples x nvariables;
this looks transposed (shape is 0 x 1)
'this looks transposed (shape is %d x %d)' % x.shape[::-1])
Traceback (most recent call last):
File "./a.py", line 11, in <module>
df_useful.hist()
File "/usr/lib/python2.7/dist-packages/pandas/tools/plotting.py", line 2075, in hist_frame
ax.hist(data[col].dropna().values, **kwds)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 8312, in hist
xmin = min(xmin, xi.min())
File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", line 21, in _amin
out=out, keepdims=keepdims)
ValueError: zero-size array to reduction operation minimum which has no identity
我确实安装了 pandas 包、numpy、matplotlib。 谢谢
【问题讨论】:
-
数据在哪里?文本文件?
-
是的。在 csv 或制表符分隔的文件中。而且它是一个大文件……大约有 10 万个条目。
-
@mane 请更新您的问题以指定数据为 CSV 格式。让未来的读者比解析您的评论更容易。在问题中提及您迄今为止尝试过的内容也很有帮助。 stackoverflow.com/help/how-to-ask
标签: python