-1

If I have column name category and in that I have rows like Plane Travel|Train Travel|Bus Travel then how can I extract Plane Travel in pandas Dataframe

  • Possible duplicate of [Select rows from a DataFrame based on values in a column in pandas](https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas) – Acorn May 05 '18 at 08:09

2 Answers2

0

You need to use the .str accessor and then .split() your string then you can put the result into separated columns.

Let's generate the proper DataFrame:

df = pd.DataFrame({"Category":["Plane France", "Train Russia", "Spacecraft Moon"],
                   "other_variable":[1,2,3] })
print df

       Category  other_variable
0  Plane France               1
1  Train Russia               2
2  Spacecraft Moon            3

You now can access strings with .straccessor (take a look at Pandas doc) and split them.

df["category_list"] = df.Category.str.split(" ") # you can replace " " with any   
                                                 # other word delimiter

and you have to then attibute each element of the list to a new column

df[["transportation", "destination"]] = pd.DataFrame(df.category_list.values.tolist(), 
                                                     index = df.index)

that gives

          Category  other_variable       category_list transportation  \
0     Plane France               1     [Plane, France]          Plane   
1     Train Russia               2     [Train, Russia]          Train   
2  Spacecraft Moon               3  [Spacecraft, Moon]     Spacecraft   

  destination  
0      France  
1      Russia  
2        Moon  

You now have your transportation an destination columns.

Adrien Pacifico
  • 1,649
  • 1
  • 15
  • 33
0

You can directly extract the first value using .str in pandas.

# sample data frame
df = pd.DataFrame({'category': ['Plane Travel|Train Travel|Bus Travel ','Plane Travel|Train Travel|Bus Travel ','Plane Travel|Train Travel|Bus Travel ']})

# new column
df['new_col'] = df['category'].str.split('|').str[0]

print(df)

                               catgeory       new_col
0  Plane Travel|Train Travel|Bus Travel   Plane Travel
1  Plane Travel|Train Travel|Bus Travel   Plane Travel
2  Plane Travel|Train Travel|Bus Travel   Plane Travel
YOLO
  • 20,181
  • 5
  • 20
  • 40