'Loading a csv with pd.read_csv and returning a df with column names parsed from the header

Given a csv like:

Given a csv like in the link

How can I load it into a pandas df (e.g., with pd.read_csv()) such that it looks like the dataframe below:

How can I load it into a pandas df (e.g., with pd.read_csv()) such that it looks like the dataframe in this link?



Solution 1:[1]

def load_csv(filename):
    category = pd.read_csv(filename, nrows=0).columns.str.replace('(\.\d+)$','')
    category_subset = pd.read_csv(filename, skiprows=1, nrows=0).columns.str.replace('\:.*','').str.replace('(\.\d+)$','')
    df = pd.read_csv(filename, header=2)
    df.columns = category + ": " + category_subset + ": " + df.columns
    return df

I came up with this. But it's so ugly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s