Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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] 
Comment

PREVIOUS NEXT
Code Example
Python :: similarity index in python 
Python :: how to python 
Python :: pandas divide multiple columns by one column 
Python :: python generic 
Python :: for each loop python 
Python :: python enumerate unique values 
Python :: collections counter sort by value 
Python :: flask api with parameter 
Python :: python loop backwards 
Python :: concatenation array 
Python :: If elif else 
Python :: plotly coordinates mapping 
Python :: default values python 
Python :: python generate html 
Python :: tqdm command that works both in notebook and lab 
Python :: numpy array deepcopy 
Python :: python convert bytes to string 
Python :: python integer to octal 
Python :: python min value index from an array 
Python :: discord.py permissions 
Python :: xml depth python 
Python :: pca 
Python :: argparse print help if no arguments 
Python :: python convert b string to dict 
Python :: how to use django-rest-framework-datatables 
Python :: how to comment text in python 
Python :: pandas resample friday 
Python :: access column pandas 
Python :: uppercase python 
Python :: django admin readonly models 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =