我有一個看起來與此類似的 df:
|email|first_name|last_name|id|group_email|
|-|-|-|-|-|
|[email protected]|drew|barry|05|[email protected]|
|[email protected]|nate|lewis|03|[email protected]|
|[email protected]|chris|ryan|04|[email protected]|
我決議出 group_code,即第三個連字符之后的子字串。我現在想將此子字串添加回每個條目的資料框中。所以 df 看起來像這樣:
|email|first_name|last_name|id|group_email|group_code|
|-|-|-|-|-|-|
|[email protected]|drew|barry|05|[email protected]|rate|
|[email protected]|nate|lewis|03|[email protected]|factor|
|[email protected]|chris|ryan|04|[email protected]|drive|
我該怎么做呢?
uj5u.com熱心網友回復:
我們試試看
df['group_code'] = (df['group_email'].str.extract('(-[^@]*){3}')[0]
.str.lstrip('-'))
print(df)
email first_name last_name id group_email group_code
0 [email protected] drew barry 5 [email protected] rate
1 [email protected] nate lewis 3 [email protected] factor
2 [email protected] chris ryan 4 [email protected] drive
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/519507.html
標籤:Python熊猫数据框
上一篇:從損壞的時間戳列中提取年份
