str.slice()

Pandas

Break the string to get substrings .
Returns Series or Index of object

Options

By using start=1 we can mark the starting of the substring. Note that first char is at position 0.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex']}
df = pd.DataFrame(data=my_dict)
print(df.NAME.str.slice(start=1))
Output
0    avi
1    aju
2    lex
Using from end or right side ( start = -1 )
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex']}
df = pd.DataFrame(data=my_dict)
print(df.NAME.str.slice(start=-1))
Output
0    i
1    u
2    x

stop

We can use stop=2 to return the substring ending at 2nd position. ( first char is at 0th position)
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex']}
df = pd.DataFrame(data=my_dict)
print(df.NAME.str.slice(stop=2))
Output
0    Ra
1    Ra
2    Al

start & stop

import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex']}
df = pd.DataFrame(data=my_dict)
print(df.NAME.str.slice(start=1,stop=3))
output
0    av
1    aj
2    le

step

Same process can be repeated by using step.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex']}
df = pd.DataFrame(data=my_dict)
print(df.NAME.str.slice(step=2))
Output
0    Rv
1    Rj
2    Ae

Using start stop step

import pandas as pd 
my_dict={'NAME':['RaviKing','RajuQueen','AlexMinister']}
df = pd.DataFrame(data=my_dict)
print(df.NAME.str.slice(start=1,stop=10,step=2))
Output
0     aiig
1     auue
2    lxiit
Pandas contains() Converting char case split()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer