How to use split expand=True to expand records in a cell to multi columns and limit it with n parameter.¶
In [5]:
import pandas as pd
df = pd.read_csv("str_split_expand_true.csv")
df
Out[5]:
In [38]:
df["name"].str.split(",", expand=True)
Out[38]:
assign it back to the dataframe¶
In [35]:
df[["First Name", "Last Name"]] = df["name"].str.split(",", expand=True)
df
Out[35]:
drop off redundant original column¶
In [34]:
df.drop("name" , axis="columns")
Out[34]:
a lot of things can be done after this point.. nunique, value_counts, catergory¶
In [60]:
df = pd.read_csv("str_split_expand_true.csv")
df[["fname","lname"]] = df["name"].str.split(",", expand=True)
df[["lname","minit"]] = df["lname"].str.split(" ", expand=True)
df["fname"].value_counts()
Out[60]:
In [61]:
df["fname"].nunique()
Out[61]:
In [68]:
import pandas as pd
df = pd.read_csv("str_split_expand_true.csv")
df["name"].str.split(",").get(0)
Out[68]:
In [ ]:
No comments:
Post a Comment