0

I have csv file that looks something like this

     x     y  z 
0  a_b_c  30 40
1  d_e_f  50 57

I am trying to modify column x by splitting the string at the first underscore and only keeping the string right before it. Based on this answer I came up with the following solution:

df['x'] = df['x'].map(lambda x: x.str.split('_').str[0])

However, i get an error message

TypeError: only integer scalar arrays can be converted to a scalar index

JM9
  • 119
  • 1
  • 8

1 Answers1

0

use

df['x'].str.split("_").str[0]

when you call df['x'].map(...), you operate each element of x column. you can't use str on an element. It can be used on the x column, a Series.

Lambda
  • 1,392
  • 1
  • 9
  • 11
  • Thank you for your reply. The error message still persists. Do i have to explicitly tell pandas its a string or something? – JM9 Oct 30 '19 at 13:57