簡體   English   中英

屬性錯誤:“str”object 沒有屬性“str”

[英]Attribute Error: 'str' object has no attribute 'str'

我有一個包含 URL 的 dataframe 列。 我想通過對每一行使用正則表達式模式從這個 URL 中提取一個特定的字符串。 這是 URL 字符串的示例:

'www.abcdef.com/sports-bra-sports-bra-black-abcde1f02-c11.html',

由於列是一個系列,我需要遍歷,我嘗試了以下代碼:

1.

for i in df['landing_screen_name']:
    regex = i.str.extract(r'.{0,13}.html')
    print(regex)
    break

2.

for idx, row in df.iterrows():
    a = row['landing_screen_name'].str.contains(r'.{0,13}.html')
    print(a)
    break

但是對於兩者我都收到以下錯誤:

AttributeError: 'str' object has no attribute 'str'

我已經嘗試了所有方法,但還沒有找到問題,您能幫我解決這個問題嗎?

嘗試這個:

df['landing_screen_name'] = df['landing_screen_name'].str.extract(r'(.{0,13}\.html)')

您應該在列級別操作,例如使用:

print(df['landing_screen_name'].str.extract(r'.{0,13}.html').to_string(index=False))

暫無
暫無

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

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