Python Pandas - 重新索引
重新索引是 Pandas 中的一项强大且基础的操作,它允许你将数据与新的标签集对齐。无论处理行还是列,重新索引都能让你控制数据如何与指定的标签对齐。
这项操作在处理时间序列数据、对齐来自不同来源的数据集,或简单地将数据重新组织以匹配特定结构时特别有用。
什么是重新索引?
Pandas 中的重新索引指的是将数据调整以匹配沿指定轴(行或列)的新标签集的过程。此过程可以完成多项任务 −
重新排序: 将现有数据重新排序以匹配新的标签集。
插入缺失值: 如果新标签集中某个标签在原始数据中不存在,Pandas 将为该标签插入缺失值 (NaN)。
填充缺失数据: 你可以指定如何填充重新索引产生的缺失值,使用各种填充方法。
reindex() 方法是 Pandas 中执行重新索引的主要工具。它允许你修改 Pandas 数据结构的行和列标签。
重新索引中使用的关键方法
reindex(): 此方法用于将现有数据结构与新 index(或 columns)对齐。它可以重新排序和/或插入缺失标签。
reindex_like(): 此方法允许你将一个 DataFrame 或 Series 重新索引以匹配另一个。适用于确保两个数据结构对齐相似时。
填充方法: 当重新索引引入 NaN 值时,你可以使用 ffill、bfill 和 nearest 等方法填充它们。
示例:重新索引 Pandas Series
以下示例演示了使用 reindex() 方法重新索引 Pandas Series 对象。在本例中,"f" 标签在原始 Series 中不存在,因此在输出的重新索引 Series 中显示为 NaN。
import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])
print("原始 Series:\n",s)
s_reindexed = s.reindex(["e", "b", "f", "d"])
print('\n输出重新索引 Series:\n',s_reindexed)
执行上述代码将得到以下输出 −
Original Series: a 0.148874 b 0.592275 c -0.903546 d 1.031230 e -0.254599 dtype: float64 Output Reindexed Series: e -0.254599 b 0.592275 f NaN d 1.031230 dtype: float64
示例:重新索引 DataFrame
考虑以下使用 reindex() 方法重新索引 DataFrame 的示例。对于 DataFrame,你可以同时重新索引行(index)和列。
import pandas as pd
import numpy as np
N=5
df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
})
print("原始 DataFrame:\n", df)
#重新索引 DataFrame
df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'B'])
print("\n输出重新索引 DataFrame:\n",df_reindexed)
其 输出 如下 −
Original DataFrame:
A x y C D
0 2016-01-01 0.0 0.513990 Medium 118.143385
1 2016-01-02 1.0 0.751248 Low 91.041201
2 2016-01-03 2.0 0.332970 Medium 100.644345
3 2016-01-04 3.0 0.723816 High 108.810386
4 2016-01-05 4.0 0.376326 High 101.346443
Output Reindexed DataFrame:
A C B
0 2016-01-01 Medium NaN
2 2016-01-03 Medium NaN
5 NaT NaN NaN
重新索引以与其他对象对齐
有时,您可能需要重新索引一个 DataFrame 以使其与另一个对齐。reindex_like() 方法可以让您无缝完成此操作。
示例
以下示例演示了如何使用 reindex_like() 方法将一个 DataFrame (df1) 重新索引以匹配另一个 DataFrame (df2)。
import pandas as pd import numpy as np df1 = pd.DataFrame(np.random.randn(10,3),columns=['col1','col2','col3']) df2 = pd.DataFrame(np.random.randn(7,3),columns=['col1','col2','col3']) df1 = df1.reindex_like(df2) print(df1)
其 输出 如下 −
col1 col2 col3
0 -2.467652 -1.211687 -0.391761
1 -0.287396 0.522350 0.562512
2 -0.255409 -0.483250 1.866258
3 -1.150467 -0.646493 -0.222462
4 0.152768 -2.056643 1.877233
5 -1.155997 1.528719 -1.343719
6 -1.015606 -1.245936 -0.295275
注意: 在这里,df1 DataFrame 被修改并像 df2 一样重新索引。如果列名不匹配,则会为整个列标签添加 NaN。
重新索引时的填充
reindex() 方法提供了一个可选参数 method 用于填充缺失值。可用的方法包括 −
pad/ffill: 向前填充值。
bfill/backfill: 向后填充值。
nearest: 从最近的索引值填充。
示例
以下示例演示了 ffill 方法的工作原理。
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randn(6, 3), columns=['col1', 'col2', 'col3'])
df2 = pd.DataFrame(np.random.randn(2, 3), columns=['col1', 'col2', 'col3'])
# Padding NaNs
print(df2.reindex_like(df1))
# Now fill the NaNs with preceding values
print("Data Frame with Forward Fill:")
print(df2.reindex_like(df1, method='ffill'))
其 输出 如下 −
col1 col2 col3
0 1.311620 -0.707176 0.599863
1 -0.423455 -0.700265 1.133371
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN
Data Frame with Forward Fill:
col1 col2 col3
0 1.311620 -0.707176 0.599863
1 -0.423455 -0.700265 1.133371
2 -0.423455 -0.700265 1.133371
3 -0.423455 -0.700265 1.133371
4 -0.423455 -0.700265 1.133371
5 -0.423455 -0.700265 1.133371
注意: 最后四行被填充了。
重新索引时填充的限制
limit 参数为重新索引时的填充提供了额外的控制。limit 指定了连续匹配的最大数量。
示例
让我们考虑以下示例来理解指定填充限制 −
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randn(2,3),columns=['col1','col2','col3'])
# Padding NaNs
print(df2.reindex_like(df1))
# Now fill the NaNs with preceding values
print("Data Frame with Forward Fill limiting to 1:")
print(df2.reindex_like(df1, method='ffill', limit=1))
其 输出 如下 −
col1 col2 col3
0 0.247784 2.128727 0.702576
1 -0.055713 -0.021732 -0.174577
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN
Data Frame with Forward Fill limiting to 1:
col1 col2 col3
0 0.247784 2.128727 0.702576
1 -0.055713 -0.021732 -0.174577
2 -0.055713 -0.021732 -0.174577
3 NaN NaN NaN
4 NaN NaN NaN
5 NaN NaN NaN
注意: 前向填充 (ffill) 仅限于一行。