-1
df1["state"] = df1["place_with_parent_names"].str.split("|",expand=True)[2]

what [2] actually indicate of a string split method.

makt
  • 89
  • 2
  • 15

2 Answers2

1

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:

enter image description here

enter image description here

DaveB
  • 452
  • 2
  • 7
-1

the split function return a list so [2] is the index of the required string

aly95
  • 1
  • You're forgetting that there is a dataframe involved: `str.split()` returns a new dataframe, and `[2]` is used on that new dataframe. – 9769953 Mar 27 '22 at 11:22
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 27 '22 at 18:31