📜  在 Pandas 系列中将 multiIndex 连接成单个索引

📅  最后修改于: 2022-05-13 01:54:24.112000             🧑  作者: Mango

在 Pandas 系列中将 multiIndex 连接成单个索引

在本文中,我们将看到如何在 Pandas Series 中将多索引连接到单个索引。多索引是指有多个同名索引。

创建示例系列:

Python3
# importing pandas module
import pandas as pd
import numpy as np
 
# Creating series data for address details
index_values = pd.Series([('sravan', 'address1'),
                          ('sravan', 'address2'),
                          ('sudheer', 'address1'),
                          ('sudheer', 'address2')])
 
# assigning values with integers
data = pd.Series(np.arange(1, 5),
                 index=index_values)
 
# display data
print(data)


Python3
# importing pandas module
import pandas as pd
 
# Creating series data for address details
index_values = pd.Series([('sravan', 'address1'),
                          ('sravan', 'address2'),
                          ('sudheer', 'address1'),
                          ('sudheer', 'address2')])
 
# assigning values with integers
data = pd.Series(np.arange(1, 5), index=index_values)
 
# display data
print(data)
 
# mapping with data using '_' symbol with join
data1 = data.index.map('_'.join)
 
print(data1)


Python3
# importing pandas module
import pandas as pd
 
# importing numpy module
import numpy as np
 
# Creating series data for address details with same name.
index_values = pd.Series([('sravan', 'address1'),
                          ('sravan', 'address2'),
                          ('sravan', 'address3'),
                          ('sravan', 'address4')])
 
# assigning values with integers
data = pd.Series(np.arange(1, 5),
                 index=index_values)
 
# display data
print(data)
 
# mapping with data using '_' symbol with join
data1 = data.index.map('_'.join)
 
print(data1)


Python3
# importing pandas module
import pandas as pd
 
# importing numpy module
import numpy as np
 
# Creating series data for address details
# with same name with nested lists.
index_values = pd.Series([['sravan', 'address1'],
                          ['sravan', 'address2'],
                          ['sravan', 'address3'],
                          ['sravan', 'address4'],
                          ['vani', 'address5'],
                          ['vani', 'address6'],
                          ['vani', 'address7'],
                          ['vani', 'address8']])
 
# assigning values with integers
data = pd.Series(np.arange(1, 9),
                 index=index_values)
 
# display data
print(data)
 
# mapping with data using '_' symbol with join
data1 = data.index.map('_'.join)
 
print(data1)


Python3
# importing pandas module
import pandas as pd
 
# importing numpy module
import numpy as np
 
# Creating series data for address details w.r.t
# college names  with same name with nested lists.
index_values = pd.Series([['sravan', 'address1', 'vignan'],
                          ['sravan', 'address2', 'vignan'],
                          ['sravan', 'address3', 'vignan'],
                          ['sravan', 'address4', 'vignan'],
                          ['vani', 'address5', 'vignan lara'],
                          ['vani', 'address6', 'vignan lara'],
                          ['vani', 'address7', 'vignan lara'],
                          ['vani', 'address8', 'vignan lara']])
 
# assigning values with integers
data = pd.Series(np.arange(1, 9),
                 index=index_values)
 
# display data
print(data)
 
# mapping with data using '/' symbol with join
data1 = data.index.map('/'.join)
 
print(data1)


输出:



连接两个或多个数据称为串联。在这里,我们将使用map函数连接索引。

句法:

以下是描述如何将多索引串联成系列中的单个索引的各种示例:

示例 1:

这段代码解释了基于多索引的地址合并。

蟒蛇3

# importing pandas module
import pandas as pd
 
# Creating series data for address details
index_values = pd.Series([('sravan', 'address1'),
                          ('sravan', 'address2'),
                          ('sudheer', 'address1'),
                          ('sudheer', 'address2')])
 
# assigning values with integers
data = pd.Series(np.arange(1, 5), index=index_values)
 
# display data
print(data)
 
# mapping with data using '_' symbol with join
data1 = data.index.map('_'.join)
 
print(data1)

输出:



示例 2:

此代码是所有给定相同名称但在元组中传递不同值的示例。

蟒蛇3

# importing pandas module
import pandas as pd
 
# importing numpy module
import numpy as np
 
# Creating series data for address details with same name.
index_values = pd.Series([('sravan', 'address1'),
                          ('sravan', 'address2'),
                          ('sravan', 'address3'),
                          ('sravan', 'address4')])
 
# assigning values with integers
data = pd.Series(np.arange(1, 5),
                 index=index_values)
 
# display data
print(data)
 
# mapping with data using '_' symbol with join
data1 = data.index.map('_'.join)
 
print(data1)

输出:

示例 3:

此代码给出了嵌套列表数据结构中给出的多个用户的演示。

蟒蛇3

# importing pandas module
import pandas as pd
 
# importing numpy module
import numpy as np
 
# Creating series data for address details
# with same name with nested lists.
index_values = pd.Series([['sravan', 'address1'],
                          ['sravan', 'address2'],
                          ['sravan', 'address3'],
                          ['sravan', 'address4'],
                          ['vani', 'address5'],
                          ['vani', 'address6'],
                          ['vani', 'address7'],
                          ['vani', 'address8']])
 
# assigning values with integers
data = pd.Series(np.arange(1, 9),
                 index=index_values)
 
# display data
print(data)
 
# mapping with data using '_' symbol with join
data1 = data.index.map('_'.join)
 
print(data1)

输出:



示例 4:

此代码解释了与在由 '/'运算符分隔的嵌套列表中传递的地址相关的大学数据。

蟒蛇3

# importing pandas module
import pandas as pd
 
# importing numpy module
import numpy as np
 
# Creating series data for address details w.r.t
# college names  with same name with nested lists.
index_values = pd.Series([['sravan', 'address1', 'vignan'],
                          ['sravan', 'address2', 'vignan'],
                          ['sravan', 'address3', 'vignan'],
                          ['sravan', 'address4', 'vignan'],
                          ['vani', 'address5', 'vignan lara'],
                          ['vani', 'address6', 'vignan lara'],
                          ['vani', 'address7', 'vignan lara'],
                          ['vani', 'address8', 'vignan lara']])
 
# assigning values with integers
data = pd.Series(np.arange(1, 9),
                 index=index_values)
 
# display data
print(data)
 
# mapping with data using '/' symbol with join
data1 = data.index.map('/'.join)
 
print(data1)

输出: