python join two columns and transform it as index

Solutions on MaxInterview for python join two columns and transform it as index by the best coders in the world

showing results for - "python join two columns and transform it as index"
Julieta
28 Sep 2020
1df = DataFrame({'var_1':['a','b','c'], 'var_2':[1, 2, 3], 'var_3':['apple', 'banana', 'pear']})
2cols = ['var_1', 'var_2']    # Set columns to combine
3df['combined'] = df[cols].apply(lambda row: ', '.join(row.values.astype(str)), axis=1)
4
5# Define which column is index
6df_i = df.set_index('combined') 
7
8# Set the index to None
9df_i.index.names = [None] 
queries leading to this page
add two columns as stringsjoin dataframes pandaspandas concatenate two columnspandas dataframe two columns to onepandas concat two dataframesmerge two columns in python and place indexinginner join two dataframe by index columnpandas dataframe join 2 dataframes same rows different indexpandas concatenate two dataframespython change 2 colums into 1 stringconcat 2 columns in pandashow to combine two columns into one in pandas 5cconcatenate two dataframes pandasmerge tables pandasjoin pandas two columnspython 2 columns to onepandas merge rows with nameadding contents of two string columns pandashow to set two columns as index in pandaspandas concatenate two dataframes with same columnscombine two columns in pandaspandas left joinpandas join column into one stringpython merge dataframe by columnmerge dataframes pandasconcatenate two columns of dataframe into 3 column pandaspandas join two columns on indexpandas join two columns into one based on indexpandas merge two columns into onemerge concat append pandasmerge dataframe pandaspandas join two dataframescombine two df columns pythonmerge dataframe in pythonhow to combine column index of two dataframespandas join two columns into one on indexpandas combine two columns number to stringjoin tables in pandasjoin 2 index columns in pandaspandas merge dataframesconcatenate two columns in dataframepandas merge multiple dataframesjoin 2 cols in pandas df and set index merge two dataframes pandaspython join two columns and transform it as index