Python Pandas 和 Numpy - 将多索引连接成单个索引
要将多索引连接成单索引,首先,让我们导入所需的Pandas和Numpy库及其各自的别名-
import pandas as pd import numpy as np
创建熊猫系列-
d = pd.Series([('Jacob', 'North'),('Ami', 'East'),('Ami', 'West'),('Scarlett', 'South'),('Jacob', 'West'),('Scarlett', 'North')])现在,使用Numpyarrange()方法-
dataFrame = pd.Series(np.arange(1, 7), index=d)
现在让我们映射并加入-
dataMap = dataFrame.index.map('_'.join)示例
以下是代码-
import pandas as pd
import numpy as np
#pandasseries
d = pd.Series([('Jacob', 'North'),('Ami', 'East'),('Ami', 'West'),('Scarlett', 'South'),('Jacob', 'West'),('Scarlett', 'North')])
dataFrame = pd.Series(np.arange(1, 7), index=d)
#mappingandjoining
dataMap = dataFrame.index.map('_'.join)
print"\nResult after mapping:\n",dataMap输出结果这将产生以下输出-
Result after mapping: Index([u'Jacob_North', u'Ami_East', u'Ami_West', u'Scarlett_South', u'Jacob_West', u'Scarlett_North'],dtype='object')