Below is the sample dataframe.
import pandas as pd
# dictionary of lists
dict1 = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
df = pd.DataFrame(dict1)
I would like to create a list of two dataframe columns in every row.
As in, I want nC2 operation on the column names and every row of the list would have two column names. Expected output list is as follows:-
output_list[0]=['name','degree']
output_list[1]=['score','degree']
output_list[2]=['name','score']
Please guide
>Solution :
Use:
from itertools import combinations
print (list(combinations(df.columns, 2)))
[('name', 'degree'), ('name', 'score'), ('degree', 'score')]
output_list = [list(x) for x in combinations(df.columns, 2)]
print(output_list)
[['name', 'degree'], ['name', 'score'], ['degree', 'score']]
output_list = list(map(list, combinations(df.columns, 2)))
print(output_list)
[['name', 'degree'], ['name', 'score'], ['degree', 'score']]