亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將特定的定位文本放入數據幀索引中?

如何將特定的定位文本放入數據幀索引中?

汪汪一只貓 2022-08-16 10:40:05
我有一個包含一些文本索引的數據幀,其中包含要復制到列表中的必要信息。我不知道文本信息具體如何(單詞總是變化),但我知道在索引中的位置:'point.subclase.optimum.R31.完成'.R31是我想寫在列表中的值,所以我知道這個文本總是不同的,介于point.subclase.optimum.和.done之間。我試過:info_list = []for col in df.columns:    if ('point.subclase.optimum.' in col) and ('.done' in col):        info_list.append(col)但是該腳本只是為我提供了列表中的整個索引。有誰知道如何解決它?
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

使用Series.str.extract與轉義,因為特殊的正則表達式字符,然后刪除可能的缺失值,如果Series.dropna不匹配,最后將輸出轉換為列表:\.

df = pd.DataFrame({'a':range(3)}, index=['point.subclase.optimum.R31.done',

                                         'point.subclase',

                                         'point.subclase.optimum.R98.done'])

print (df)

                                 a

point.subclase.optimum.R31.done  0

point.subclase                   1

point.subclase.optimum.R98.done  2


L = (df.index.str.extract(r'point\.subclase\.optimum\.(.*)\.done', expand=False)

             .dropna()

             .tolist())

print (L)

['R31', 'R98']




查看完整回答
反對 回復 2022-08-16
  • 1 回答
  • 0 關注
  • 99 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號