📜  python - 连接两列并将其转换为索引 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:01.576000             🧑  作者: Mango

代码示例1
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]