Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于组计数的数据帧过滤_Python_Pandas - Fatal编程技术网

Python 基于组计数的数据帧过滤

Python 基于组计数的数据帧过滤,python,pandas,Python,Pandas,我的数据框如下所示: item_id sales_quantity 1 10 1 11 1 1 1 2 ... ... 10 1 10 9 ... ... 我想过滤掉出现次数少于100次的项目id对应的所有行。以

我的数据框如下所示:

item_id       sales_quantity
   1              10             
   1              11 
   1              1 
   1              2
   ...            ...
   10             1
   10             9
   ...            ...
我想过滤掉出现次数少于100次的
项目id
对应的所有行。以下是我尝试过的:

from pandas import *
from statsmodels.tsa.stattools import adfuller
def adf(X):

    result = adfuller(X)
    print('ADF Statistic: %f' % result[0])
    print('p-value: %f' % result[1])
    print('Critical Values:')
    for key, value in result[4].items():
        print('\t%s: %.3f' % (key, value))


filtered = df.groupby('item_id_copy')['sales_quantity'].filter(lambda x: len(x) >= 100)
df[df['sales_quantity'].isin(filtered)]
df['sales_quantity'].groupby(df['item_id_copy']).apply(adf)
但是,当我运行以下命令时:
df['sales\u quantity'].groupby(df['item\u id\u copy']).size()
,我得到很多小于100的item\u id。有人能告诉我我的代码有什么问题吗?

看来您需要删除
['sales\u quantity']

df = df.groupby('item_id_copy').filter(lambda x: len(x) >= 100)
或:

样本:

np.random.seed(130)
df=pd.DataFrame(np.random.randint(3, size=(10,2)), columns=['item_id_copy','sales_quantity'])
print (df)
   item_id_copy  sales_quantity
0             1               1
1             1               2
2             2               1
3             0               1
4             2               0
5             2               0
6             0               1
7             1               2
8             1               2
9             1               2

df1 = df.groupby('item_id_copy').filter(lambda x: len(x) >= 4)
print (df1)
   item_id_copy  sales_quantity
0             1               1
1             1               2
7             1               2
8             1               2
9             1               2

df1 = df[df.groupby('item_id_copy')['sales_quantity'].transform('size') >= 4]
print (df1)
   item_id_copy  sales_quantity
0             1               1
1             1               2
7             1               2
8             1               2
9             1               2
编辑:

对于应用后的列,可以添加
Series
constructor,然后按重新整形。上次从列
临界值中的dicts创建新的
数据帧
,并将其转换为原始:

np.random.seed(130)
df = pd.DataFrame(np.random.randint(10, size=(1000,2)), 
                  columns=['item_id_copy','sales_quantity'])
#print (df)

from statsmodels.tsa.stattools import adfuller

def adf(X):
    result = adfuller(X)
    return pd.Series(result, index=['ADF Statistic','p-value','a','b','Critical Values','c'])

df1 = df[df.groupby('item_id_copy')['sales_quantity'].transform('size') >= 100]


df2 = df1['sales_quantity'].groupby(df1['item_id_copy']).apply(adf).unstack()
df3 = pd.DataFrame(df2['Critical Values'].values.tolist(),
                   index=df2.index, 
                   columns=['1%','5%','10%'])
df2=df2[['ADF Statistic','p-value']].join(df3).reset_index()
print (df2)
   item_id_copy ADF Statistic      p-value        1%        5%       10%
0             1      -12.0739   2.3136e-22 -3.498198 -2.891208 -2.582596
1             2      -4.48264  0.000211343 -3.494850 -2.889758 -2.581822
2             7       -4.2745  0.000491609 -3.491818 -2.888444 -2.581120
3             9      -11.7981  9.47089e-22 -3.486056 -2.885943 -2.579785

当我运行上述语句(
df['sales\u quantity'])时,我看到
item\u id
计数大于和小于100。groupby(df['item\u id\u copy'])。size()
)Hmmm,我不确定是否理解<使用
groupby
的code>Filter
应按条件返回所有分组。这对你不起作用吗?我添加了一些示例,给我一些时间。你答案中的第二个选项有效,但与
>100
一起使用。我需要删除出现次数少于100次的项目ID。谢谢。你说得对,在第一个解决方案中,我有输入错误-无法定义列
sales\u quantity
。请参见编辑。你是对的,需要
=100
np.random.seed(130)
df = pd.DataFrame(np.random.randint(10, size=(1000,2)), 
                  columns=['item_id_copy','sales_quantity'])
#print (df)

from statsmodels.tsa.stattools import adfuller

def adf(X):
    result = adfuller(X)
    return pd.Series(result, index=['ADF Statistic','p-value','a','b','Critical Values','c'])

df1 = df[df.groupby('item_id_copy')['sales_quantity'].transform('size') >= 100]


df2 = df1['sales_quantity'].groupby(df1['item_id_copy']).apply(adf).unstack()
df3 = pd.DataFrame(df2['Critical Values'].values.tolist(),
                   index=df2.index, 
                   columns=['1%','5%','10%'])
df2=df2[['ADF Statistic','p-value']].join(df3).reset_index()
print (df2)
   item_id_copy ADF Statistic      p-value        1%        5%       10%
0             1      -12.0739   2.3136e-22 -3.498198 -2.891208 -2.582596
1             2      -4.48264  0.000211343 -3.494850 -2.889758 -2.581822
2             7       -4.2745  0.000491609 -3.491818 -2.888444 -2.581120
3             9      -11.7981  9.47089e-22 -3.486056 -2.885943 -2.579785