Nolohice
Nolohice

Reputation: 339

Pandas slicing every row item of a column

I have a data frame and in the second column there are strings. I can get the second column a number of ways:

df.iloc[:,1]
df.ix[:,1]
df['column_name']

and I can get certain rows by indexing further, df.iloc[:,1][:4]

But what I can't seem to do is slice every string at once in the column. Something like df.iloc[:,1][:][1] returns the first row of the column but df.iloc[:,1][1][1] does actually give me a slice. How do I apply this to all rows of the column without a for loop?

Upvotes: 1

Views: 3075

Answers (1)

EdChum
EdChum

Reputation: 394041

You can use the vectorised str methods to slice each string on each row

So

df['column_name'].str[1]

Will return the 2nd word in each row

Upvotes: 2

Related Questions