Python - 将 Pandas 类型转换为 set
要将 pandas 类型转换为 Set,请使用set(). 首先,让我们创建一个 DataFrame -
dataFrame = pd.DataFrame({
"EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'],
"Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North']
}
)
对熊猫进行类型转换以进行设置,然后进行设置联合 -
set(dataFrame.EmpName) | set(dataFrame.Zone)
示例
以下是完整的代码 -
import pandas as pd输出结果# 创建数据帧
dataFrame = pd.DataFrame(
{
"EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'],
"Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North']
}
)
print"DataFrame ...\n",dataFrame
# 熊猫成套
print"\nTypecast Pandas into Set...\n",set(dataFrame.EmpName) | set(dataFrame.Zone)
这将产生以下输出 -
DataFrame ...EmpName Zone
0 John North
1 Ted South
2 Jacob South
3 Scarlett East
4 Ami West
5 Ted East
6 Scarlett North
Typecast Pandas into Set...
set(['Ami', 'East', 'North', 'West', 'Ted', 'South', 'Jacob', 'John', 'Scarlett'])
以上是 Python - 将 Pandas 类型转换为 set 的全部内容, 来源链接: utcz.com/z/356359.html