找到关于 Python 的10786 篇文章

Python Pandas - 两个索引对象的并集

AmitDiwan
更新于 2021年10月14日 06:55:11

108 次浏览

要形成两个索引对象的并集,请在 Pandas 中使用 index1.union(index2) 方法。首先,导入所需的库 −import pandas as pd 创建两个 Pandas 索引 −index1 = pd.Index([10, 20, 30, 40, 50]) index2 = pd.Index([80, 65, 60, 70, 55]) 显示 Pandas 索引 1 和索引 2 −print("Pandas Index1...", index1) print("Pandas Index2...", index2) 执行并集 −res = index1.union(index2) 示例以下是代码 −import pandas as pd # 创建两个 Pandas 索引 index1 = pd.Index([10, 20, 30, 40, 50]) index2 = pd.Index([80, 65, 60, 70, 55]) # 显示 Pandas 索引 1 和索引 2 print("Pandas Index1...", index1) print("Pandas Index2...", index2) # ... 阅读更多

Python Pandas - 获取 Period 对象所属月份的天数总数

AmitDiwan
更新于 2021年10月14日 06:55:17

641 次浏览

要获取 Period 对象所属月份的天数总数,请使用 period.daysinmonth 属性。首先,导入所需的库 −import pandas as pd pandas.Period 表示一段时间。创建两个 Period 对象 −period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = 2, day = 14, hour = 2, minute = 35) 显示 Period 对象 −print("Period1...", period1) print("Period2...", period2) 从两个 Period 对象获取月份的天数 −res1 = period1.daysinmonth res2 = period2.daysinmonth 示例以下是代码 −import pandas as pd # pandas.Period 表示一段时间 # 创建两个 ... 阅读更多

Python Pandas - 形成两个索引对象的交集并排序结果

AmitDiwan
更新于 2021年10月14日 06:52:58

567 次浏览

要形成两个索引对象的交集,请在 Pandas 中使用 index1.intersection(index2) 方法。要对结果进行排序,请使用 sort 参数。首先,导入所需的库 −import pandas as pd 创建 Pandas 索引 1 和索引 2 −index1 = pd.Index([4, 3, 2, 1]) index2 = pd.Index([8, 2, 6, 4]) 显示 Pandas 索引 1 和索引 2 print("Pandas Index1...", index1) print("Pandas Index2...", index2) 执行交集。使用“sort”参数对结果进行排序 res = index1.intersection(index2, sort=None) 示例以下是代码 −import pandas as pd # 创建 Pandas 索引 1 和索引 2 index1 = pd.Index([4, 3, 2, 1]) index2 = pd.Index([8, 2, 6, 4]) # 显示 ... 阅读更多

Python Pandas - 从 Period 对象获取一年中的某一天

AmitDiwan
更新于 2021年10月14日 06:52:27

153 次浏览

要从 Period 对象获取一年中的某一天,请使用 period.dayofyear 属性。首先,导入所需的库 −import pandas as pd pandas.Period 表示一段时间。创建两个 Period 对象 −period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = 7, day = 16, hour = 2, minute = 35) 显示 Period 对象 −print("Period1...", period1) print("Period2...", period2) 从两个 Period 对象获取一年中的某一天 −res1 = period1.dayofyear res2 = period2.dayofyear 示例以下是代码 −import pandas as pd # pandas.Period 表示一段时间 # 创建两个 Period 对象 period1 = pd.Period("2020-09-23") period2 = ... 阅读更多

Python Pandas - 获取 Period 对象所属的星期几

AmitDiwan
更新于 2021年10月14日 06:51:33

108 次浏览

要获取 Period 对象所属的星期几,请使用 period.dayofweek 属性 首先,导入所需的库 −import pandas as pd pandas.Period 表示一段时间。创建两个 Period 对象 −period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55) 显示 Period 对象 −print("Period1...", period1) print("Period2...", period2) 从两个 Period 对象获取星期几 −res1 = period1.dayofweek res2 = period2.dayofweek 示例以下是代码 −import pandas as pd # pandas.Period 表示一段时间 # 创建两个 Period 对象 period1 = ... 阅读更多

Python Pandas - 形成两个索引对象的交集

AmitDiwan
更新于 2021年10月14日 06:50:43

158 次浏览

要形成两个索引对象的交集,请在 Pandas 中使用 index1.intersection(index2) 方法。首先,导入所需的库 −import pandas as pd 创建两个 Pandas 索引 −index1 = pd.Index([10, 20, 30, 40, 50]) index2 = pd.Index([80, 65, 60, 70, 55]) 显示 Pandas 索引 1 和索引 2 −print("Pandas Index1...", index1) print("Pandas Index2...", index2) 执行交集 −res = index1.intersection(index2) 示例以下是代码 −import pandas as pd # 创建两个 Pandas 索引 index1 = pd.Index([10, 20, 30, 40, 50]) index2 = pd.Index([80, 65, 60, 70, 55]) # 显示 Pandas 索引 1 和索引 2 print("Pandas Index1...", index1) print("Pandas Index2...", index2) # ... 阅读更多

Python Pandas - 获取 Period 对象所属的月份中的某一天

AmitDiwan
更新于 2021年10月14日 06:49:36

175 次浏览

要获取 Period 对象所属的月份中的某一天,请使用 period.day 属性。首先,导入所需的库 −import pandas as pd pandas.Period 表示一段时间。创建两个 Period 对象 −period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55) 从两个 Period 对象获取月份中的某一天 −res1 = period1.day res2 = period2.day 示例以下是代码 −import pandas as pd # pandas.Period 表示一段时间 # 创建两个 Period 对象 period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, ... 阅读更多

Python Pandas - 将多个索引选项组合在一起

AmitDiwan
更新于 2021年10月14日 06:48:32

425 次浏览

要将多个索引选项组合在一起,请在 Pandas 中使用 append() 方法。首先,导入所需的库 −import pandas as pd 创建 Pandas 索引 −index1 = pd.Index([10, 20, 30, 40, 50]) 显示 Pandas 索引 −print("Pandas Index...", index1) 创建要追加的新索引 −index2 = pd.Index([60, 70, 80]) 追加新索引 −print("After appending...", index1.append(index2)) 示例以下是代码 −import pandas as pd # 创建 Pandas 索引 index1 = pd.Index([10, 20, 30, 40, 50]) # 显示 Pandas 索引 print("Pandas Index...", index1) # 返回索引中元素的数量 print("Number of elements in ... 阅读更多

Python Pandas - 获取 Timedelta 对象持续时间的总秒数

AmitDiwan
更新于 2021年10月14日 06:46:33

16K+ 次浏览

要获取 Timedelta 对象持续时间的总秒数,请使用 timedelta.total_seconds() 方法。首先,导入所需的库 −import pandas as pd TimeDeltas 是 Python 的标准 datetime 库,它使用不同的表示形式 timedelta,创建 Timedelta 对象 −timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns') 显示 Timedelta −print("Timedelta...", timedelta) 获取总秒数 −timedelta.total_seconds() 示例以下是代码 −import pandas as pd # TimeDeltas 是 Python 的标准 datetime 库,它使用不同的表示形式 timedelta # 创建 Timedelta 对象 timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms ... 阅读更多

Python Pandas - 对索引值排序并返回对索引进行排序的索引

AmitDiwan
更新于 2021年10月14日 06:45:16

224 次浏览

要对索引值进行排序并返回对索引进行排序的索引,请使用 index.sort_values()。return_indexer 参数设置为 True。首先,导入所需的库 −import pandas as pd 创建 Pandas 索引 −index = pd.Index([50, 10, 70, 95, 110, 90, 30]) 显示 Pandas 索引 −print("Pandas Index...", index) 对索引值进行排序。默认情况下,按升序排序。使用值为 True 的“return_indexer”参数返回对索引进行排序的索引 −print("Sort and also return the indices that would sort the index...", index.sort_values(return_indexer=True)) 示例以下是代码 −import pandas as pd # 创建 Pandas 索引 index = ... 阅读更多

广告
© . All rights reserved.