簡體   English   中英

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

[英]How to get an specific located text into a dataframe index?

我有一個帶有一些文本索引的數據框,其中包含我想復制到列表中的必要信息。

我不知道文本信息具體如何(這個詞總是改變),但我知道位於索引中的位置:

'point.subclass.optimum。 R31 .done' 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)

但是該腳本只是為我提供了列表中的整個索引。

有誰知道如何解決它?

使用帶有轉義\\. 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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM