Python Pandas - 返回使用掩码设置的值的新索引
要返回使用掩码设置的值的新索引,请在 Pandas 中使用 index.putmask() 方法。首先,导入必要的库——
import pandas as pd
创建 Pandas 索引——
index = pd.Index([5, 65, 10, 17, 75, 40])
显示 Pandas 索引——
print("Pandas Index...\n",index)屏蔽并用值 111 放置索引值小于 3——
print("\nMask...\n",index.putmask(index < 30, 111))
示例
以下是代码——
import pandas as pd
# Creating Pandas index
index = pd.Index([5, 65, 10, 17, 75, 40])
# Display the Pandas index
print("Pandas Index...\n",index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# mask and place index values less than 3 with a value 111
print("\nMask...\n",index.putmask(index < 30, 111))输出
这将产生以下输出——
Pandas Index... Int64Index([5, 65, 10, 17, 75, 40], dtype='int64') Number of elements in the index... 6 The dtype object... int64 Mask... Int64Index([111, 65, 111, 111, 75, 40], dtype='int64')
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP