循环遍历matplotlib中dataframe列图的列表

时间:2016-06-24 10:33:43

标签: python pandas matplotlib

我一直试图遍历这个19000元组的列表并使用matplot lib根据数据框中列的键值来绘制它们但我无法将其绘制出来。

import os
import pandas as pd
os.chdir('/home/xyzcsv')


%matplotlib inline
from pylab import *
with open('list_tuple.txt','rb') as file:
    a = file.readlines()


df = pd.read_csv('20130831_000000.csv')

def createtuple(cola,colb):
    names = df.cola
    names1 = df.colb
    X = []
    y = []
    for i in range(len(names)):
        if names[i] <=float(0) or names1[i]<=float(0):
            pass
        else:
            X.append([names1[i],names[i]])
            y.append(i+1)
    X = np.array(X)
    y = np.array(y)
    return (X,y)

def plotgraph():
    plt.figure(figsize=(10, 6))
    plt.scatter(X[:,0], X[:,1],c=y.astype(np.float),alpha=.5)
    plt.show()

for i in range(len(a)):
    b = a[i].split("'")
    (X,y) = createtuple(b[1],b[3])
    plotgraph()

我得到的错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-11551e9515fa> in <module>()
     31     b = a[i].split("'")
     32     print b
---> 33     (X,y) = createtuple(b[1],b[3])
     34     plotgraph()

<ipython-input-12-11551e9515fa> in createtuple(cola, colb)
      9 
     10 def createtuple(cola,colb):
---> 11     names = df.cola
     12     names1 = df.colb
     13     X = []

/home/bigdata/anaconda2/lib/python2.7/site-packages/pandas/core/generic.pyc in __getattr__(self, name)
   2667             if name in self._info_axis:
   2668                 return self[name]
-> 2669             return object.__getattribute__(self, name)
   2670 
   2671     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'cola'

如何使用字符串连接调用dataframe列?

1 个答案:

答案 0 :(得分:0)

使用下标按字符串名称返回列:

names = df[cola]
names1 = df[colb]

尝试将cols作为属性访问它失败,因为cola不是属性,但是如果你将列名传递给下标,那么它将返回该列

此外,我强烈建议养成永远不会将列作为属性访问的习惯,因为它会产生奇怪的行为,尤其是当列名与现有属性或方法匹配时,例如,您有一个列名sum然后调用df.sum返回方法sum()

的地址