# import Pandas as pd
import pandas as pd
# innitilize Dataframe
df = pd.DataFrame(
"Email": [
"Alex.jhon@gmail.com",
"Hamza.Azeez@gmail.com",
"Harry.barton@hotmail.com",
"Number": ["+44-3844556210", "+44-2245551219", "+44-1049956215"],
"Location": ["Alameda,California", "Sanford,Florida", "Columbus,Georgia"],
print("Dataframe series:\n", df)
Split 'Number' column into two individual columns :
0 1
0 +44 3844556210
1 +44 2245551219
2 +44 1049956215
如果我們只使用擴充套件引數 Series.str.split(expand=True),這將允許拆分空格,但不能用 - 和 , 或字串中存在的任何正規表示式進行分隔,你必須通過 pat 引數。
讓我們重新命名這些拆分列。
df[["Dialling Code", "Cell-Number"]] = df.Number.str.split("-", expand=True)
print(df)
我們建立了兩個新系列 Dialling code 和 Cell-Number 並使用 Number 系列分配值。
Email Number Location Dialling Code \
0 Alex.jhon@gmail.com +44-3844556210 Alameda,California +44
1 Hamza.Azeez@gmail.com +44-2245551219 Sanford,Florida +44
2 Harry.barton@hotmail.com +44-1049956215 Columbus,Georgia +44
Cell-Number
0 3844556210
1 2245551219
2 1049956215
示例 2:
在這個例子中,我們將用 , 分割 Location 系列。
df[["City", "State"]] = df.Location.str.split(",", expand=True)
print(df)
拆分 Location 系列並將其值儲存在單獨的系列 City 和 State 中。
Email Number Location City \
0 Alex.jhon@gmail.com +44-3844556210 Alameda,California Alameda
1 Hamza.Azeez@gmail.com +44-2245551219 Sanford,Florida Sanford
2 Harry.barton@hotmail.com +44-1049956215 Columbus,Georgia Columbus
State
0 California
1 Florida
2 Georgia
讓我們看看最後一個例子。我們將在 Email 系列中分隔全名。
full_name = df.Email.str.split(pat="@", expand=True)
print(full_name)
0 1
0 Alex.jhon gmail.com
1 Hamza.Azeez gmail.com
2 Harry.barton hotmail.com
現在我們用 . 分隔名字和姓氏。
df[["First Name", "Last Name"]] = full_name[0].str.split(".", expand=True)
print(df)
Email Number Location First Name \
0 Alex.jhon@gmail.com +44-3844556210 Alameda,California Alex
1 Hamza.Azeez@gmail.com +44-2245551219 Sanford,Florida Hamza
2 Harry.barton@hotmail.com +44-1049956215 Columbus,Georgia Harry
Last Name
0 jhon
1 Azeez
2 barton
如果在 .split() 方法中傳遞了 expand=True,n=-1 引數將不起作用。
print(df["Email"].str.split("@", n=-1, expand=True))
0 1
0 George Washington
1 Hamza Azeez
2 Harry Walker
整個示例程式碼如下。
# import Pandas as pd
import pandas as pd
# create a new Dataframe
df = pd.DataFrame(
"Email": [
"Alex.jhon@gmail.com",
"Hamza.Azeez@gmail.com",
"Harry.barton@hotmail.com",
"Number": ["+44-3844556210", "+44-2245551219", "+44-1049956215"],
"Location": ["Alameda,California", "Sanford,Florida", "Columbus,Georgia"],
print("Dataframe series :\n", df)
print(
"\n\nSplit 'Number' column by '-' into two individual columns :\n",
df.Number.str.split(pat="-", expand=True),
df[["Dialling Code", "Cell-Number"]] = df.Number.str.split("-", expand=True)
print(df)
df[["City", "State"]] = df.Location.str.split(",", expand=True)
print(df)
full_name = df.Email.str.split(pat="@", expand=True)
print(full_name)
df[["First Name", "Last Name"]] = full_name[0].str.split(".", expand=True)
print(df)
Dataframe series :
Email Number Location
0 Alex.jhon@gmail.com +44-3844556210 Alameda,California
1 Hamza.Azeez@gmail.com +44-2245551219 Sanford,Florida
2 Harry.barton@hotmail.com +44-1049956215 Columbus,Georgia
Split 'Number' column by '-' into two individual columns :
0 1
0 +44 3844556210
1 +44 2245551219
2 +44 1049956215
Email Number Location Dialling Code \
0 Alex.jhon@gmail.com +44-3844556210 Alameda,California +44
1 Hamza.Azeez@gmail.com +44-2245551219 Sanford,Florida +44
2 Harry.barton@hotmail.com +44-1049956215 Columbus,Georgia +44
Cell-Number
0 3844556210
1 2245551219
2 1049956215
Email Number Location Dialling Code \
0 Alex.jhon@gmail.com +44-3844556210 Alameda,California +44
1 Hamza.Azeez@gmail.com +44-2245551219 Sanford,Florida +44
2 Harry.barton@hotmail.com +44-1049956215 Columbus,Georgia +44
Cell-Number City State
0 3844556210 Alameda California
1 2245551219 Sanford Florida
2 1049956215 Columbus Georgia
0 1
0 Alex.jhon gmail.com
1 Hamza.Azeez gmail.com
2 Harry.barton hotmail.com
Email Number Location Dialling Code \
0 Alex.jhon@gmail.com +44-3844556210 Alameda,California +44
1 Hamza.Azeez@gmail.com +44-2245551219 Sanford,Florida +44
2 Harry.barton@hotmail.com +44-1049956215 Columbus,Georgia +44
Cell-Number City State First Name Last Name
0 3844556210 Alameda California Alex jhon
1 2245551219 Sanford Florida Hamza Azeez
2 1049956215 Columbus Georgia Harry barton
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
如何將 Pandas DataFrame 列標題獲取為列表
如何刪除 Pandas DataFrame 列
如何在 Pandas 中將 DataFrame 列轉換為日期時間
如何在 Pandas DataFrame 中將浮點數轉換為整數
如何按一列的值對 Pandas DataFrame 進行排序
如何用 group-by 和 sum 獲得 Pandas 總和