![](/img/trans.png)
[英]How to check if a character is in a specific position in a string in Python?
[英]How to check if a cell has a specific character in specific position in Pandas
我有一個 pandas dataframe 像這樣:
東風:
uid maskeduid
VEH12345L0 72647hghghghg
VEH12323L3 hgh5454jjbjjb
VEH11145M4 jhj24j3j5bjnj
VEH78345L3 12kjkkndw2knk
VEH31345N3n 145jhjhjbvrkl
我想得到一個 dataframe df1 其中每個單元格的第 9 個字符 ='L'
df1:
uid maskeduid
VEH12345L0 72647hghghghg
VEH12323L3 hgh5454jjbjjb
VEH78345L3 12kjkkndw2knk
如何在內置 function 中使用任何 pandas 來實現?
Python 從0
開始計數,因此對於 select 9th
值使用str[8]
,通過Series.eq
與L
進行比較並在boolean indexing
中進行過濾
df1 = df[df['uid'].str[8].eq('L')]
print (df1)
uid maskeduid
0 VEH12345L0 72647hghghghg
1 VEH12323L3 hgh5454jjbjjb
3 VEH78345L3 12kjkkndw2knk
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.