2014-01-14 241 views
0
从计数方法访问数据

我从一个数据框使用计数方法这种方式返回信息:在大熊猫

df = pd.DataFrame.from_csv(csv_file) 

for i in df['OPTION'].unique(): 
    count = df.loc[df['OPTION'] == i].count 
    print count 

这将返回:

DatetimeIndex:4641个条目,2014-01- 08 02:02:05.740845到2014年1月8日02:58:56.405287

数据列(总3列):

OPTION 4641非空值

SELL 4641的非空值

BUY 4641的非空值

dtypes:float64(2),对象(1)>

哪个是种信息我之后,但我想访问数据(如本例中的数字4641)或我的代码中的“非空值”,而不是将其打印出来。我应该如何访问这种数据?

回答

1

首先,您正在有效地创建数据的groups。所以这更好地服务于以下。

grouped = df.groupby('OPTION') 

接下来,您希望从此grouped对象中获得特定的组。因此,您遍历组,提取次数(这基本上是指数的长度),提取特定的列(如,沽售)

for name, group in grouped: 
    print("Option name: {}".format(name)) 
    # Count of entries for this OPTION 
    print("Count: {}".format(len(group.index))) 
    # Accessing specific columns, say SELL 
    print("SELL for this option\n") 
    print(group["SELL"]) 
    # Summary for SELL for this option 
    print("Summary\n") 
    print(group["SELL"].describe()) 

的一个很好的参考聚集型分相结合的官方Pandas文档。 从相同的引用。

 
By “group by” we are referring to a process involving one or more of the following steps 
Splitting the data into groups based on some criteria 
Applying a function to each group independently 
Combining the results into a data structure 
+0

完美,这样做更有意义,谢谢 –