我是熊貓新手。我有一個像這樣的資料集:
df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'],
'Event':['music.example.com', 'poetry.example.com', 'theatre.domain.com', 'comedy.domain.com'],
'Cost':[10000, 5000, 15000, 2000]})
并且想為“基域”添加一列,這樣我就可以在基域而不是子域上執行聚合函式。在此示例中,新列將具有值
'baseDomain':['example.com', 'example.com', 'domain.com', 'domain.com'],
它不應該只是一味地對“。”進行拆分。所以可能應該使用類似的東西,tld盡管域不是 URL
========== 更新
使用 adhg 和 Henry Ecker 解決方案并這樣做:
def get_base_domain(event):
ext = tldextract.extract(event)
return ext.domain '.' ext.suffix
df['baseDomain'] = df.apply(lambda x: get_base_domain(x['Event']), axis=1)
uj5u.com熱心網友回復:
你可以這樣做:
def get_base_domain(event):
return event[event.index('.') 1:]
df['baseDomain'] = df.apply(lambda x: get_base_domain(x['Event']), axis=1)
期望的結果:
Date Event Cost baseDomain
0 10/2/2011 music.example.com 10000 example.com
1 11/2/2011 poetry.example.com 5000 example.com
2 12/2/2011 theatre.domain.com 15000 domain.com
3 13/2/2011 comedy.domain.com 2000 domain.com
get_base_domain如果您有不干凈的事件域資料,請進行調整
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/435569.html
上一篇:在兩個資料集中用ID替換名稱
下一篇:用isin切片python資料幀
