Despite spending half the day on Stack Overflow, I have not found a solution. Working in python 3.9.0, I need to clean a dataframe. The first row should be transposed into a column, the second row needs to be made a header, and the repeating columns ('political_rights', 'civil_liberties, 'status') need to be simplified into only 3 columns. This can be done by making the values in the column "country" repeat for each year. Whenever I accomplish one thing, I mess up another so any help/advice is deeply appreciated!
Simiplified version of current dataframe (actual df: 207 rows × 148 columns):
df_bad = pd.DataFrame({'col1': ['years', 'country', 'Afghanistan', 'Albania', 'Algeria', 'Andorra'],
'col2': [1972, 'political_rights', 4, 7, 6, 4],
'col3': [1972, 'civil_liberties', 5, 7, 6, 3],
'col4': [1972, 'status', 'PF', 'NF', 'NF', 'NF'],
'col5': [1973, 'political_rights', 7, 7, 6, 4],
'col6': [1973, 'civil_liberties', 6, 7, 6, 4],
'col7': [1973, 'status', 'NF', 'NF', 'NF', 'PF']})
Simiplified version of desired dataframe (future df: 10250 rows × 5 columns):
df = pd.DataFrame({'country': ['Afghanistan', 'Albania', 'Algeria', 'Afghanistan', 'Albania', 'Algeria'],
'years': [1972, 1972, 1972, 1973, 1973, 1973],
'political_rights': [4, 7, 6, 7, 7, 6],
'civil_liberties': [5, 7, 6, 6, 7, 6],
'status': ['PF', 'NF', 'NF', 'NF', 'NF', 'NF']})