import pandas as pd
# https://stackoverflow.com/questions/30791839/is-there-an-easy-way-to-group-columns-in-a-pandas-dataframe
from io import StringIO
import numpy as np
a = pd.read_csv(StringIO('T,Ax,Ay,Az,Bx,By,Bz,Cx,Cy,Cz,Dx,Dy,Dz\n\
0,1,2,1,3,2,1,4,2,1,5,2,1\n\
1,8,2,3,3,2,9,9,1,3,4,9,1\n\
2,4,5,7,7,7,1,8,3,6,9,2,3'))
a.set_index('T', inplace=True)
a
a.columns = pd.MultiIndex.from_tuples([(c[0], c[1]) for c in a.columns])
a
row = []
acol = ['AAA', 'BBB']
bcol = ['A', 'B', 'C', 'D']
ccol = ['x', 'y', 'z']
for i in range(10):
col = []
col.append(str(i))
for a in acol:
for b in bcol:
for c in ccol:
col.append(str(random.randint(1,10)))
row.append(col)
df = pd.DataFrame(row)
df.set_index(0, inplace=True)
df
df.columns = pd.MultiIndex.from_tuples([[a,b,c] for a in acol for b in bcol for c in ccol])
df
# importing pandas as pd
import pandas as pd
# Creating the array
array =[[1, 2, 3], ['Sharon', 'Nick', 'Bailey'],
['Doctor', 'Scientist', 'Physicist']]
# Print the array
print(array)
midx = pd.MultiIndex.from_arrays(array,
names =('Ranking', 'Names', 'Profession'))
# Print the MultiIndex
print(midx)
import random
import pandas as pd
row = []
cols0 = ['Source1', 'Source2']
cols1 = ['status', 'op_status', 'env']
for a in range(10):
vals = {}
for b in cols0:
for c in cols1:
vals[b] = [*vals.get(b, []), str(random.randint(10, 20))]
row.append([str(a), *vals['Source1'], *vals['Source2']])
df = pd.DataFrame(row)
df.columns = ['IP', *[f"{a} {b}" for a in cols0 for b in cols1]]
df = df.set_index('IP')
df.head(3)
df.columns = pd.MultiIndex.from_tuples([c.split(" ") for c in df.columns])
df
df.columns
import random
import pandas as pd
row = []
for a in range(10):
for b in ['status', 'op_status', 'env']:
row.append([str(a), b, random.randint(1,10), random.randint(10, 20)])
df = pd.DataFrame(row)
df.columns = ['IP', 'Attr', 'Source1', 'Source2']
df.head(10)
df.groupby(['IP', 'Attr']).first().to_excel("a.xlsx")
df.groupby(['IP', 'Attr']).first().to_html("a.xlsx")