2016-04-28 30 views
1

我有一個數據幀,其中有兩列,分別爲SESSIONPRICE_POINT在熊貓數據框的列中存儲特定值的計數

SESSION是類別變量(與各種IP會話值)

PRICE_POINT具有兩個值,例如 '高', '低'

我運行如下:

n = pd.value_counts(df['price_point'].values, sort=False) 

我的輸出是,

high 30204 
low  62978 
dtype: int64 

我需要的是,我想分別從「price_point」列中獲得「高」和「低」的計數,並將該值存儲爲n = 30204和m = 62978.

任何想法?

+0

IIUC不這只是'high = n.index ['high']'和'low = n.index ['low']'? – EdChum

回答

0

如何:

n = df.price_point.value_counts().high 
m = df.price_point.value_counts().low 

df = pd.DataFrame(data={'price':['high', 'high', 'low', 'low', 'low', 'low']}) 
df.price.value_counts().high 

2 

,或者在兩個步驟:

counts = df.price_point.value_counts() 
n = counts.high 
+0

謝謝@Stefan。這工作 – Anu

0

IIUC那麼你只指數使用的值,例如系列:

In [94]: 
df = pd.DataFrame({'col1':['high','high','low','low','low','low']}) 
df['col1'].value_counts() 

Out[94]: 
low  4 
high 2 
Name: col1, dtype: int64 

In [96]: 
counts.index 

Out[96]: 
Index(['low', 'high'], dtype='object') 

In [97]:  
counts = df['col1'].value_counts() 
print('high: ', counts['high'], 'low: ', counts['low']) 

high: 2 low: 4 
相關問題