python - No results are returned in dataframe -


i trying take average every fifth , every sixth row of var in dataframe, , put result in new column var b. nan shows. may resulted did not return values correctly?

here sample data:

pid         1      0 1      3 1      2 1      6 1      0 1      2 2      3 2      3 2      1 2      4 2      0 2      4 

expected results:

pid         b   1      0      1 1      3      1 1      2      1 1      6      1 1      0      1 1      2      1 2      3      2 2      3      2 2      1      2 2      4      2 2      0      2 2      4      2 

my codes:

lst1 = df.iloc[5::6, :]  lst2 = df.iloc[4::6, :]    df['b'] = (lst1['a'] + lst2['a'])/2 print(df['b']) 

the script can run without error, var b empty , show nan. help!

there problem data not aligned, because different indexes, nans.

print(lst1)     pid  5     1  2 11    2  4  print(lst2)     pid  4     1  0 10    2  0  print (lst1['a'] + lst2['a']) 4    nan 5    nan 10   nan 11   nan name: a, dtype: float64 

solution use values add series numpy array:

print (lst1['a'] + (lst2['a'].values)) 5     2 11    4 name: a, dtype: int64 

or can sum 2 numpy arrays:

print (lst1['a'].values + (lst2['a'].values)) [2 4] 

it seems need:

df['b'] = (lst1['a'] + lst2['a'].values).div(2) df['b'] = df['b'].bfill() print(df)     pid     b 0     1  0  1.0 1     1  3  1.0 2     1  2  1.0 3     1  6  1.0 4     1  0  1.0 5     1  2  1.0 6     2  3  2.0 7     2  3  2.0 8     2  1  2.0 9     2  4  2.0 10    2  0  2.0 11    2  4  2.0 

but if need mean of 5. , 6. value per group pid use groupby transform:

df['b']  = df.groupby('pid').transform(lambda x: x.iloc[[4, 5]].mean()) print(df)     pid     b 0     1  0  1.0 1     1  3  1.0 2     1  2  1.0 3     1  6  1.0 4     1  0  1.0 5     1  2  1.0 6     2  3  2.0 7     2  3  2.0 8     2  1  2.0 9     2  4  2.0 10    2  0  2.0 11    2  4  2.0 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -