2014-01-14 166 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

完美,這樣做更有意義,謝謝 –