df1["state"] = df1["place_with_parent_names"].str.split("|",expand=True)[2]
what [2] actually indicate of a string split method.
df1["state"] = df1["place_with_parent_names"].str.split("|",expand=True)[2]
what [2] actually indicate of a string split method.
A Minimal, Reproducible Example would have been useful, but imagine that your code looks something like this (note I haven't included the [2]
yet):
import pandas as pd
df_1 = pd.DataFrame(columns=["place_with_parent_names"], data = ['A|B|C','D_E_F','G|H|I'])
df_1["place_with_parent_names"].str.split("|",expand=True)
Running the code generates this result, so adding a [2]
at the end simply slices to column 2 as shown:
the split function return a list so [2] is the index of the required string