在Pandas系列中把多索引串联成单一索引

在Pandas系列中把多索引串联成单一索引

在这篇文章中,我们将看到如何在Pandas系列中把多个索引串联到一个单一的索引。多索引是指拥有多个同名的索引。

创建一个样本系列。

# 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)

输出:

在Pandas系列中把多索引串联成单一索引

连接两个或更多的数据被称为连接(concatenation)。在这里,我们将使用map函数来连接索引。

语法:

map(fun, iter)

  • fun: 函数
  • iter: 迭代。

下面是各种例子,描述了如何在Series中把多个索引串联成一个索引。

示例 1:

这段代码解释了基于多指数的地址连接成一个。

# 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)

输出:

在Pandas系列中把多索引串联成单一索引

示例 2:

这段代码是对所有给定的相同名称,但在一个元组中传递不同价值的例子。

# 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)

输出:

在Pandas系列中把多索引串联成单一索引

示例 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)

输出:

在Pandas系列中把多索引串联成单一索引

示例 4:

这段代码解释了学院的数据,涉及到由’/’操作符分隔的嵌套列表中传递的地址。

# 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)

输出:

在Pandas系列中把多索引串联成单一索引

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程