Consider:
x = data.frame(c('ABCD', 'EFGH'), row.names=c('1A', '1B'))
I need a substring of each element in the data frame. Something like this:
substring(x, 2,4)
Using “contains” to Find a Substring in a Pandas DataFrame The contains method in Pandas allows you to search a column for a specific substring. The contains method returns boolean values for the Series with True for if the original Series value contains the substring and False if not.
Method 3: Slice Columns in pandas using iloc[] The iloc is present in the Pandas package. The iloc can be used to slice a Dataframe using indexing. df. iloc[] method is used when the index label of a data frame is something other than numeric series of 0, 1, 2, 3….
You could use sapply
:
sapply(x, substring, 2, 4)
or, if you want to take only one specific column - say #1 - of the data frame:
substring(x[,1], 2, 4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With