網(wǎng)站首頁(yè) 編程語言 正文
重置索引(reindex)可以更改原 DataFrame 的行標(biāo)簽或列標(biāo)簽,并使更改后的行、列標(biāo)簽與 DataFrame 中的數(shù)據(jù)逐一匹配。通過重置索引操作,您可以完成對(duì)現(xiàn)有數(shù)據(jù)的重新排序。如果重置的索引標(biāo)簽在原 DataFrame 中不存在,那么該標(biāo)簽對(duì)應(yīng)的元素值將全部填充為 NaN。
重置行列標(biāo)簽
看一組簡(jiǎn)單示例:
import pandas as pd
import numpy as np
N=20
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()
})
#重置行、列索引標(biāo)簽
df_reindexed = df.reindex(index=[0,2,5], columns=['A', 'C', 'B'])
print(df_reindexed)
輸出結(jié)果:
? ? ? ? ? ?A ? ? ? C ? B
0 2020-12-07 ?Medium NaN
2 2020-12-09 ? ? Low NaN
5 2020-12-12 ? ?High NaN
現(xiàn)有 a、b 兩個(gè) DataFrame 對(duì)象,如果想讓 a 的行索引與 b 相同,您可以使用 reindex_like() 方法。示例如下:
import pandas as pd
import numpy as np
a = pd.DataFrame(np.random.randn(10,3),columns=['col1','col2','col3'])
b = pd.DataFrame(np.random.randn(7,3),columns=['col1','col2','col3'])
a= a.reindex_like(b)
print(a)
輸出結(jié)果:
? ? ? col1 ? ? ?col2 ? ? ?col3
0 ?1.776556 -0.821724 -1.220195
1 -1.401443 ?0.317407 -0.663848
2 ?0.300353 -1.010991 ?0.939143
3 ?0.444041 -1.875384 ?0.846112
4 ?0.967159 ?0.369450 -0.414128
5 ?0.320863 -1.223477 -0.337110
6 -0.933665 ?0.909382 ?1.129481
上述示例,a 會(huì)按照 b 的形式重建行索引。需要特別注意的是,a 與 b 的列索引標(biāo)簽必須相同。
填充元素值
reindex_like() 提供了一個(gè)可選的參數(shù)method,使用它來填充相應(yīng)的元素值,參數(shù)值介紹如下:
pad/ffill:向前填充值;
bfill/backfill:向后填充值;
nearest:從距離最近的索引值開始填充。
示例如下:
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'])
#使df2和df1行標(biāo)簽相同
print(df2.reindex_like(df1))
#向前填充
print(df2.reindex_like(df1,method='ffill'))
輸出結(jié)果:
#填充前
? ? ? ?col1 ? ? ?col2 ? ? ?col3
0 ?0.129055 ?0.835440 ?0.383065
1 -0.357231 ?0.379293 ?1.211549
2 ? ? ? NaN ? ? ? NaN ? ? ? NaN
3 ? ? ? NaN ? ? ? NaN ? ? ? NaN
4 ? ? ? NaN ? ? ? NaN ? ? ? NaN
5 ? ? ? NaN ? ? ? NaN ? ? ? NaN
#填充后
? ? ? ?col1 ? ? ?col2 ? ? ?col3
0 ?0.129055 ?0.835440 ?0.383065
1 -0.357231 ?0.379293 ?1.211549
2 -0.357231 ?0.379293 ?1.211549
3 -0.357231 ?0.379293 ?1.211549
4 -0.357231 ?0.379293 ?1.211549
5 -0.357231 ?0.379293 ?1.211549
限制填充行數(shù)
reindex_like() 還提供了一個(gè)額外參數(shù) limit,該參數(shù)用來控制填充的最大行數(shù)。示例如下:
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'])
print (df2.reindex_like(df1))
#最多填充2行
print (df2.reindex_like(df1,method='ffill',limit=2))
輸出結(jié)果:
? ? ? col1 ? ? ?col2 ? ? ?col3
0 -1.829469 ?0.310332 -2.008861
1 -1.038512 ?0.749333 -0.094335
2 ? ? ? NaN ? ? ? NaN ? ? ? NaN
3 ? ? ? NaN ? ? ? NaN ? ? ? NaN
4 ? ? ? NaN ? ? ? NaN ? ? ? NaN
5 ? ? ? NaN ? ? ? NaN ? ? ? NaN? ? ? ?col1 ? ? ?col2 ? ? ?col3
0 -1.829469 ?0.310332 -2.008861
1 -1.038512 ?0.749333 -0.094335
2 -1.038512 ?0.749333 -0.094335
3 -1.038512 ?0.749333 -0.094335
4 ? ? ? NaN ? ? ? NaN ? ? ? NaN
5 ? ? ? NaN ? ? ? NaN ? ? ? NaN
由上述示例可以看出,填充了 2、3 行 缺失值,也就是只填充了 2 行數(shù)據(jù)。
重命名標(biāo)簽
rename() 方法允許您使用某些映射(dict或Series)或任意函數(shù)來對(duì)行、列標(biāo)簽重新命名,示例如下:
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randn(6,3),columns=['col1','col2','col3'])
print (df1)
#對(duì)行和列重新命名
print (df1.rename(columns={'col1' : 'c1', 'col2' : 'c2'},index = {0 : 'apple', 1 : 'banana', 2 : 'durian'}))
輸出結(jié)果:
? ? ? ?col1 ? ? ?col2 ? ? ?col3
0 -1.762133 -0.636819 -0.309572
1 -0.093965 -0.924387 -2.031457
2 -1.231485 -0.738667 ?1.415724
3 -0.826322 ?0.206574 -0.731701
4 ?1.863816 -0.175705 ?0.491907
5 ?0.677361 ?0.870041 -0.636518? ? ? ? ? ? ? c1 ? ? ? ?c2 ? ? ?col3
apple ?-1.762133 -0.636819 -0.309572
banana -0.093965 -0.924387 -2.031457
durian -1.231485 -0.738667 ?1.415724
3 ? ? ?-0.826322 ?0.206574 -0.731701
4 ? ? ? 1.863816 -0.175705 ?0.491907
5 ? ? ? 0.677361 ?0.870041 -0.636518
rename() 方法提供了一個(gè) inplace 參數(shù),默認(rèn)值為 False,表示拷貝一份原數(shù)據(jù),并在復(fù)制后的數(shù)據(jù)上做重命名操作。若 inplace=True 則表示在原數(shù)據(jù)的基礎(chǔ)上重命名。
原文鏈接:https://blog.csdn.net/ccc369639963/article/details/124246373
相關(guān)推薦
- 2022-09-18 Pycharm快速安裝OpenCV的詳細(xì)操作步驟_python
- 2022-05-10 關(guān)于react中的state整理
- 2022-07-23 Go語言學(xué)習(xí)筆記之文件讀寫操作詳解_Golang
- 2023-10-14 WIN32 預(yù)定義宏WIN32,_WIN32,_WIN64介紹使用
- 2023-04-26 React高級(jí)指引之Refs?and?the?DOM使用時(shí)機(jī)詳解_React
- 2024-03-14 ThreadLocal使用,配合攔截器HandlerInterceptor使用
- 2022-07-08 python基礎(chǔ)知識(shí)之索引與切片詳解_python
- 2023-05-22 pytorch的Backward過程用時(shí)太長(zhǎng)問題及解決_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支