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

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

如何使用 extract 從 pandas 數據框中提取大寫字母以及一些子字符串?

如何使用 extract 從 pandas 數據框中提取大寫字母以及一些子字符串?

慕雪6442864 2023-12-12 14:46:41
這個問題是上一個問題How to extract only uppercase substring from pandas series? 的后續問題 。我決定提出新問題,而不是改變舊問題。我的目標是從名為 item 的列中提取聚合方法agg和特征名稱。feat這是問題:import numpy as npimport pandas as pddf = pd.DataFrame({'item': ['num','bool', 'cat', 'cat.COUNT(example)','cat.N_MOST_COMMON(example.ord)[2]','cat.FIRST(example.ord)','cat.FIRST(example.num)']})regexp = (r'(?P<agg>) '     # agg is the word in uppercase (all other substring is lowercased)         r'(?P<feat>), '   # 1. if there is no uppercase, whole string is feat                           # 2. if there is uppercase the substring after example. is feat                           # e.g. cat ==> cat                           # cat.N_MOST_COMMON(example.ord)[2] ==> ord                          )df[['agg','feat']] = df.col.str.extract(regexp,expand=True)# I am not sure how to build up regexp here.print(df)"""Required output                                item   agg               feat0                                num                     num1                               bool                     bool2                                cat                     cat3                 cat.COUNT(example)   COUNT                           # note: here feat is empty4  cat.N_MOST_COMMON(example.ord)[2]   N_MOST_COMMON     ord5             cat.FIRST(example.ord)   FIRST             ord6             cat.FIRST(example.num)   FIRST             num""";
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

對于feat,由于您已經在其他 StackOverflow 問題中得到了答案agg,我認為您可以使用以下內容根據兩個不同的模式提取兩個不同的系列,這些模式彼此分開|,然后fillna()一個系列與另一個系列分開。


^([^A-Z]*$)僅當完整字符串為小寫時才返回完整字符串

[^a-z].*example\.([a-z]+)\).*$example.僅當之前的)字符串中有大寫字母時才應返回之后和之前的字符串example.

df = pd.DataFrame({'item': ['num','bool', 'cat', 'cat.COUNT(example)','cat.N_MOST_COMMON(example.ord)[2]','cat.FIRST(example.ord)','cat.FIRST(example.num)']})


s = df['item'].str.extract('^([^A-Z]*$)|[^a-z].*example\.([a-z]+)\).*$', expand=True)

df['feat'] = s[0].fillna(s[1]).fillna('')

df

Out[1]: 

                                item  feat

0                                num   num

1                               bool  bool

2                                cat   cat

3                 cat.COUNT(example)      

4  cat.N_MOST_COMMON(example.ord)[2]   ord

5             cat.FIRST(example.ord)   ord

6             cat.FIRST(example.num)   num

上面給出了您正在尋找樣本數據的輸出,并符合您的條件。然而:


如果后面有大寫怎么辦example.?電流輸出將返回''

請參見下面的示例#2,其中一些數據根據上述點進行了更改:


df = pd.DataFrame({'item': ['num','cat.count(example.AAA)', 'cat.count(example.aaa)', 'cat.count(example)','cat.N_MOST_COMMON(example.ord)[2]','cat.FIRST(example.ord)','cat.FIRST(example.num)']})


s = df['item'].str.extract('^([^A-Z]*$)|[^a-z].*example\.([a-z]+)\).*$', expand=True)

df['feat'] = s[0].fillna(s[1]).fillna('')

df

Out[2]: 

                                item                    feat

0                                num                     num

1             cat.count(example.AAA)                        

2             cat.count(example.aaa)  cat.count(example.aaa)

3                 cat.count(example)      cat.count(example)

4  cat.N_MOST_COMMON(example.ord)[2]                     ord

5             cat.FIRST(example.ord)                     ord

6             cat.FIRST(example.num)                     num


查看完整回答
反對 回復 2023-12-12
  • 1 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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