How to make for efficent nested loop with a pandas dataframe

Viewed 204

the below python code works with a pandas dataframe but I was wondering how to make it more efficient.

columns = list(data)
lenn=len(columns)
lenn

i=0
while i<lenn:
    j=i+1
    while j <lenn:
        print(data.iloc[:,i]*data.iloc[:,j])
        #print(data.iloc[:,j])
        j=j+1
    i=i+1
1 Answers

We can do shift

data.shift(axis=1)*data

Update 1

s=data.shift(axis=1)*data
for x in list(s):
    print(s.loc[:,s])

Update 2

for x in list(data):
    print(data.loc[:,x],data.shift(axis=1).loc[:,x])

Update 3

import itertools
t=list(itertools.combinations(range(df.shape[1]), 2))
for x in t:
    print(data.iloc[:,x[0]],data.iloc[:,x[1]])
Related