Split and merge 2 columns pandas
Question:
Answers:
Create DataFrame by constructor with MultiIndex.from_product
:
data = [('Mary',20, 'John', 22)]
df = pd.DataFrame(data,
columns=pd.MultiIndex.from_product([['MALE','FEMALE'],['NAME','AGE']]))
Or use header=[0,1]
if need MultiIndex
from header in excel file:
df = pd.read_excel(file, index_col=[0], header=[0,1])
Create DataFrame by constructor with MultiIndex.from_product
:
data = [('Mary',20, 'John', 22)]
df = pd.DataFrame(data,
columns=pd.MultiIndex.from_product([['MALE','FEMALE'],['NAME','AGE']]))
Or use header=[0,1]
if need MultiIndex
from header in excel file:
df = pd.read_excel(file, index_col=[0], header=[0,1])