looping into dates and apply function to pandas da

2019-07-29 20:43发布

I'm trying to detect the first dates when an event occur: here in my dataframe for the product A (see pivot table) I have 20 items stored for the first time on 2017-04-03.

so I want to create a new variable calle new_var_2017-04-03 that store the increment. On the other hand on the next day 2017-04-04 I don't mind if the item is now 50 instead of 20, I only want to store only the 1st event

It gives me several errors, I would like to know at least if the entire logic behind it makes sense, it's "pythonic", or if I'm completeley on the wrong way

raw_data = {'name': ['B','A','A','B'],'date' : pd.to_datetime(pd.Series(['2017-03-30','2017-03-31','2017-04-03','2017-04-04'])),
    'age': [10,20,50,30]}
df1 = pd.DataFrame(raw_data, columns = ['date','name','age'])


table=pd.pivot_table(df1,index=['name'],columns=['date'],values=['age'],aggfunc='sum')
table

I'm passing the dates to a list

dates=df1['date'].values.tolist()

I want to do a backward loop into my list "dates" and create a variable if an event occurs. pseudo code: with i-1 I mean the item before i in the list

def my_fun(x,list):
    for i in reversed(list):
        if (x[i]-x[i-1])>0 :
            x[new_var+i]=x[i]-x[i-1]
    else:
        x[new_var+i]=0
return x  

print (df.apply(lambda x: my_fun(x,dates), axis=1))

desidered output:

raw_data2 = {'new_var': ['new_var_2017-03-30','new_var_2017-03-31','new_var_2017-04-03','new_var_2017-04-04'],'result_a': [np.nan,20,np.nan,np.nan],'result_b': [10,np.nan,np.nan,np.nan]}
df2= pd.DataFrame(raw_data2, columns = ['new_var','result_a','result_b'])

df2.T

1条回答
相关推荐>>
2楼-- · 2019-07-29 20:50

Let's try this:

df1['age'] = df1.groupby('name')['age'].transform(lambda x: (x==x.min())*x)
df1.pivot_table(index='name', columns='date', values='age').replace(0,np.nan)


date  2017-03-30  2017-03-31  2017-04-03  2017-04-04
name                                                
A            NaN        20.0         NaN         NaN
B           10.0         NaN         NaN         NaN
查看更多
登录 后发表回答