python – join two columns and transform it as index
df = DataFrame({'var_1':['a','b','c'], 'var_2':[1, 2, 3], 'var_3':['apple', 'banana', 'pear']}) cols = ['var_1', 'var_2'] # Set columns to combine df['combined'] = df[cols].apply(lambda row: ', '.join(row.values.astype(str)), axis=1) # Define which column is index df_i = df.set_index('combined') # Set the index to None df_i.index.names = [None]
Here is what the above code is Doing:
1. Create a DataFrame with 3 columns and 3 rows.
2. Define the columns to combine.
3. Create a new column called ‘combined’ that combines the values of the columns defined in step 2.
4. Set the index to the ‘combined’ column.
5. Set the index name to None.