Python技术迷

Pandas 教程-重新索引

整理: python架构师

Pandas 中的 reindex 主要任务是将 DataFrame 调整为新索引,可选择填充逻辑,并将 NA/NaN 放置在以前索引中不存在值的位置。除非新索引生成为当前索引的等效索引,否则它将返回一个新对象,并且 copy 的值将变为 False。 重新索引用于更改 DataFrame 的行和列的索引。我们可以使用 reindex() 方法重新索引单个或多个行。如果在 DataFrame 中不存在,默认情况下会在新索引中分配 NaN 值。

语法:

DataFrame.reindex(labels=None, index=None, columns=None, axis=None, method=None, copy=True, level=None, fill_value=nan, limit=None, tolerance=None)  

参数:

labels: 这是一个可选参数,指的是要符合由 'axis' 指定的轴的新标签或索引。 index, columns: 这也是一个可选参数,指的是新标签或索引。它通常为索引对象,以避免重复数据。 axis: 这也是一个可选参数,它指向轴,可以是轴名称或数字。 method: 这也是一个可选参数,用于填充重新索引的 DataFrame 中的空洞。它仅适用于 DataFrame 或具有单调递增/递减顺序的 Series。 None: 这是默认值,不填充缺口。 pad / ffill: 用于将最后一个有效观测向前传播到下一个有效观测。 backfill / bfill: 用于使用下一个有效观测来填补缺口。 nearest: 用于使用下一个有效观测来填补缺口。 copy: 它的默认值为 True,返回一个新对象作为布尔值,即使传递的索引相同。 level: 它用于在级别之间广播,并在传递的 MultiIndex 级别上匹配索引值。 fill_value: 它的默认值为 np.NaN,用于填充现有的缺失(NaN)值。它需要任何新元素,以便在计算之前成功地与此值对齐 DataFrame。 limit: 它定义要前向或后向填充的最大连续元素数。 tolerance: 它也是一个可选参数,用于确定原始标签和新标签之间的最大距离,以进行不精确匹配。在匹配位置,索引的值应最满足方程 abs(index[indexer] ? target) <= tolerance。

返回:

它返回重新索引的 DataFrame。
专属福利 
👉点击领取:最全Python资料合集

示例 1:

下面的示例展示了 reindex() 函数的工作原理,以重新索引 DataFrame。在新索引中,默认值是 NaN,如果它在 DataFrame 中不存在相应的记录。

注意:我们可以使用 fill_value 来填充缺失值。

import pandas as pd  # Create dataframe  info = pd.DataFrame({"P":[4, 7, 1, 8, 9],                      "Q":[6, 8, 10, 15, 11],                      "R":[17, 13, 12, 16, 14],                      "S":[15, 19, 7, 21, 9]},                      index =["Parker", "William", "Smith", "Terry", "Phill"])   # Print dataframe  info
输出:
         A    B    D    EParker  NaN  NaN  NaN  NaNWilliam NaN  NaN  NaN  NaNSmith   NaN  NaN  NaN  NaNTerry   NaN  NaN  NaN  NaNPhill   NaN  NaN  NaN  NaN
现在,我们可以使用 dataframe.reindex() 函数来重新索引数据框。
# reindexing with new index values   info.reindex(["A", "B", "C", "D", "E"])
输出:
P   Q   R   SA   NaN NaN NaN NaNB   NaN NaN NaN NaNC   NaN NaN NaN NaND   NaN NaN NaN NaNE   NaN NaN NaN NaN
请注意,新的索引中填充了 NaN 值。我们可以使用 fill_value 参数填充缺失值。
# filling the missing values by 100   info.reindex(["A", "B", "C", "D", "E"], fill_value =100)
输出:
P   Q   R   SA   100 100 100 100B   100 100 100 100C   100 100 100 100D   100 100 100 100E   100 100 100 100

示例 2:

这个例子展示了 reindex() 函数的工作原理,以重新索引列轴。
# importing pandas as pd  importpandas as pd  # Creating the first dataframe    info1 =pd.DataFrame({"A":[1, 5, 3, 4, 2],                       "B":[3, 2, 4, 3, 4],                       "C":[2, 2, 7, 3, 4],                       "D":[4, 3, 6, 12, 7]})   # reindexing the column axis with   # old and new index values   info.reindex(columns =["A", "B", "D", "E"])
输出:
        A     B    D    EParker  NaN  NaN  NaN  NaNWilliam NaN  NaN  NaN  NaNSmith   NaN  NaN  NaN  NaNTerry   NaN  NaN  NaN  NaNPhill   NaN  NaN  NaN  NaN
请注意,在重新索引后的新列中存在 NaN 值,我们可以使用 fill_value 参数从函数中删除 NaN 值。
# reindex the columns   # fill the missing values by 25   info.reindex(columns =["A", "B", "D", "E"], fill_value =37)
输出:
        A   B   D   EParker  37  37  37  37William 37  37  37  37Smith   37  37  37  37Terry   37  37  37  37Phill   37  37  37  37



1
 
热门推荐