Pandas入门之六:重建索引
已信任 Jupyter 服务器: 本地 Python 3: Not Started [11] import pandas as pd import numpy as np [13] df = pd.DataFrame({ 'a':pd.date_range(start='2021-07-14', periods=5, freq='D'), 'b':[1,2,3,4,5], 'c':[0.1,0.2,0.3,0.4,0.5] }) df a b c 0 2021-07-14 1 0.1 1 2021-07-15 2 0.2 2 2021-07-16 3 0.3 3 2021-07-17 4 0.4 4 2021-07-18 5 0.5 [14] # 重建索引reindex df.reindex(index=[0,2,4], columns=['a','b','d']) a b d 0 2021-07-14 1 NaN 2 2021-07-16 3 NaN 4 2021-07-18 5 NaN [16] df.reindex(index=[0,5,6], columns=['a','b','c'],method='ffill')# 向前填充,5向前是4,所以为 5 0.5 a b c 0 2021-07-14 1 0.1 5 2021-07-18 5 0.5 6 2021-07-18 5 0.5 [18] df1 = pd.DataFrame({ 'g':pd.date_range(start='2021-07-14', periods=5, freq='D'), 'b':[1,2,3,4,5], 'c':[0.1,0.2,0.3,0.4,0.5] }) df1 g b c 0 2021-07-14 1 0.1 1 2021-07-15 2 0.2 2 2021-07-16 3 0.3 3 2021-07-17 4 0.4 4 2021-07-18 5 0.5 [19] # 将df索引修改为像df1的索引 df.reindex_like(df1) g b c 0 NaN 1 0.1 1 NaN 2 0.2 2 NaN 3 0.3 3 NaN 4 0.4 4 NaN 5 0.5 [20] df1 g b c 0 2021-07-14 1 0.1 1 2021-07-15 2 0.2 2 2021-07-16 3 0.3 3 2021-07-17 4 0.4 4 2021-07-18 5 0.5 [22] # 重命名索引 df1.rename(columns=({'g':'f','b':'hello','c':'world'})) f hello world 0 2021-07-14 1 0.1 1 2021-07-15 2 0.2 2 2021-07-16 3 0.3 3 2021-07-17 4 0.4 4 2021-07-18 5 0.5 [-]
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步