Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas mean of n columns

N = 9
df['new'] = df['col2'].shift(-N+1)
df.loc[0, 'new'] = df.iloc[:N, df.columns.get_loc('col2')].mean()

print (df)
      col2  desired_output        new
0   14.375       14.261111  14.261111
1   14.350       14.175000  14.175000
2   14.300       14.125000  14.125000
3   14.350       14.150000  14.150000
4   14.300       13.974000  13.974000
5   14.225       14.074000  14.074000
6   14.175       14.099000  14.099000
7   14.150       13.949000  13.949000
8   14.125       13.899000  13.899000
9   14.175       13.849000  13.849000
10  14.125       13.749000  13.749000
11  14.150       13.774000  13.774000
12  13.974             NaN        NaN
13  14.074             NaN        NaN
14  14.099             NaN        NaN
15  13.949             NaN        NaN
16  13.899             NaN        NaN
17  13.849             NaN        NaN
18  13.749             NaN        NaN
19  13.774             NaN        NaN
Comment

PREVIOUS NEXT
Code Example
Python :: |= operator python 
Python :: dumps function in json python 
Python :: pydrive download by url 
Python :: how to create an app under a folder in django 
Python :: id3 algorithm code in python 
Python :: python lambda function use global variable 
Python :: cascade models in django 
Python :: accessing a variable from outside the function in python 
Python :: scrapy get text custom tags 
Python :: Tree: Postorder Traversal 
Python :: how to make a new column with explode pyspark 
Python :: pip install 
Python :: boto3 python s3 
Python :: python find dir 
Python :: is python easy or hard to learn 
Python :: calculating auc 
Python :: python dataframe show row 
Python :: image hashing 
Python :: semicolon python 
Python :: explicitly free memory in Python code 
Python :: .lstrip() 
Python :: find location of max value in python list 
Python :: only split from third delimiter python 
Python :: Python difference between filter and map 
Python :: create sqlite table in python 
Python :: python reverse a list 
Python :: call javascript function flask 
Python :: pip vs conda 
Python :: python compiler online 
Python :: python MAX_INT 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =