找到 10786 篇文章 关于 Python

Python Pandas CategoricalIndex - 使用类似字典的新类别重命名类别

AmitDiwan
更新于 2021-10-18 06:50:12

96 次浏览

要使用类似字典的新类别重命名类别,请在 Pandas 中使用 CategoricalIndex rename_categories() 方法。首先,导入所需的库 - import pandas as pd CategoricalIndex 只能采用有限的,通常是固定的,可能值的数量。使用“ordered”参数将分类视为有序 - catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) 显示 CategoricalIndex - print("CategoricalIndex...", catIndex) 重命名类别。设置将替换旧类别的新的类似字典的类别 print("CategoricalIndex after renaming categories...", catIndex.rename_categories({'p': 5, 'q': 10, 'r': 15, 's': 20})) 示例以下代码 - import pandas as pd # CategoricalIndex can ... 阅读更多

Python Pandas CategoricalIndex - 重命名类别

AmitDiwan
更新于 2021-10-18 06:47:25

236 次浏览

要重命名类别,请在 Pandas 中使用 CategoricalIndex rename_categories() 方法。首先,导入所需的库 - import pandas as pd CategoricalIndex 只能采用有限的,通常是固定的,可能值的数量。使用“categories”参数设置分类的类别。使用“ordered”参数将分类视为有序 - catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) 重命名类别。设置将替换旧类别的新的类别 - print("CategoricalIndex after renaming categories...", catIndex.rename_categories([5, 10, 15, 20])) 示例以下代码 - import pandas as pd # CategoricalIndex can only take ... 阅读更多

Python Pandas CategoricalIndex - 检查类别是否具有有序关系

AmitDiwan
更新于 2021-10-18 06:44:28

75 次浏览

要检查类别是否具有有序关系,请使用 CategoricalIndex 的 ordered 属性。首先,导入所需的库 - import pandas as pd 使用“categories”参数设置分类的类别。使用“ordered”参数将分类视为有序 - catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) 显示 Categorical Index - print("Categorical Index...", catIndex) 获取类别 - print("DisplayingCategories from CategoricalIndex...", catIndex.categories) 检查类别是否有序关系 - print("Does categories have ordered relationship...", catIndex.ordered) 示例以下代码 - import pandas as pd # CategoricalIndex can only take on a limited, and usually ... 阅读更多

Python Pandas CategoricalIndex - 获取此分类的类别

AmitDiwan
更新于 2021-10-18 06:41:04

381 次浏览

要获取此分类的类别,请在 Pandas 中使用 CategoricalIndex 的 categories 属性。首先,导入所需的库 - import pandas as pd CategoricalIndex 只能采用有限的,通常是固定的,可能值的数量(类别。使用“categories”参数设置分类的类别。使用“ordered”参数将分类视为有序 - catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) 显示 Categorical Index - print("Categorical Index...", catIndex) 获取类别 - print("Displaying Categories from CategoricalIndex...", catIndex.categories) 示例以下代码 - import pandas as pd # CategoricalIndex can only ... 阅读更多

Python Pandas CategoricalIndex - 获取此分类的类别代码

AmitDiwan
更新于 2021-10-18 06:38:25

307 次浏览

要获取此分类的类别代码,请在 Pandas 中使用 CategoricalIndex 的 codes 属性。首先,导入所需的库 - import pandas as pd CategoricalIndex 只能采用有限的,通常是固定的,可能值的数量(类别)。使用“categories”参数设置分类的类别。使用“ordered”参数将分类视为有序。代码是整数数组,它们是 categories 数组中实际值的所在位置 - catIndex = pd.CategoricalIndex(["p", "q", "r", "s", "p", "q", "r", "s"], ordered=True, categories=["p", "q", "r", "s"]) 显示 Categorical Index - print("Categorical Index...", catIndex) 获取 ... 阅读更多

Python 中对给定列表中的所有元素进行排序并将其合并为字符串的程序

Arnab Chakraborty
更新于 2021-10-16 11:24:52

201 次浏览

假设我们给定一个正整数列表。我们必须以降序对列表进行排序,然后必须连接其中的所有元素以形成一个字符串。我们返回连接后的字符串。因此,如果输入类似于 input = [415, 78, 954, 123, 5],则输出将为 954785415123 要解决此问题,我们将遵循以下步骤 - 定义一个函数 cmp()。这将采用 l、r 如果 (l 的字符串表示 + r 的字符串表示) 的整数值 > (r 的字符串表示 + l 的字符串表示) 的整数值,则返回 1 否则,返回 ... 阅读更多

Python 中在矩形区域中找到一条不触碰炸弹的连续路径的程序

Arnab Chakraborty
更新于 2021-10-16 11:20:16

226 次浏览

假设我们给定一个数组 mat,其中元素采用 [p、q、r] 的形式,其中 p、q 是几何坐标,r 是半径值。数组中的项目是给定宽度 w 的矩形区域中炸弹的位置。矩形无限长,并以 x 坐标 x = 0 到 x = w 为界。炸弹位置中的 r 值表示炸弹的安全半径,这意味着小于炸弹半径的任何东西都会使其失效。因此,我们必须做的是 ... 阅读更多

Python 中确定是否可以通过给定点从当前位置到达某个点的程序

Arnab Chakraborty
更新于 2021-10-16 11:15:48

241 次浏览

假设在二维空间中,一个指针位于坐标为 (px、py) 的点 p 处。现在指针必须移动到另一个坐标为 (qx、qy) 的点 q。指针不能自由移动,如果某些点位于其之间,则它可以移动到 q。我们给定一个点数组“paths”,其中包含各种坐标点。如果指针位于当前指针位置的 (x+1、y) 或 (x、y+1) 或 (x-1、y) 或 (x、y-1) 处,则它可以移动到该点。数组中给定的点 ... 阅读更多

Python 中计算游戏中可收集的最大点数的程序

Arnab Chakraborty
更新于 2021-10-16 11:11:56

208 次浏览

假设我们正在玩纸牌游戏。我们得到几张线性排列的卡片,每张卡片上都有一个数字。卡片上的数字是随机分布的;并且在卡片的开头和结尾,插入了两张数字为 1 的卡片。现在,在游戏中,我们必须通过拾取给定的卡片来收集最大点数。卡片在数组“cards”中表示,其中数组中的元素表示卡片的数量 cards[i]。当我们拾取卡片 i 时,我们收集点数 cards[i - 1] * cards[i] ... 阅读更多

Python 中确定我们是否在游戏中获胜的程序

Arnab Chakraborty
更新于 2021-10-16 11:09:01

486 次浏览

假设我们正在玩一个两人游戏,其中有 n 个弹珠,并且在每一轮中,玩家必须拿走正方形数量的弹珠。如果玩家无法拿走该正方形数量的弹珠,则他/她输掉。因此,给定一个数字 n,我们必须确定我们是否可以赢得游戏。我们始终先进行轮次并选择最佳数量的弹珠。因此,如果输入类似于 14,则输出将为 True。因为在第一轮中,我们拿走了 9 个弹珠。这留下了 5 个弹珠,其中 ... 阅读更多

广告

© . All rights reserved.