如何保留系列的最后四个字母?
how to keep the four last letters of a series?
我有一系列的单词,我只想保留最后四个字母:
X = data['stem']
0 abalanz
1 abander
2 abandon
3 abanic
4 abarat
3029 best
3030 bib
3031 bolb
3032 Laz
3033 zurz
我尝试了str.replace但是对系列没影响
X = pd.Series(X).str.replace('[-4:]', '', regex=False)
我尝试将系列转换为列表,然后对其进行操作,但这只保留了列表的最后四项。
test = [X]
plop = [x[-4:] for x in test]
[3030 bib
3031 bolb
3032 Laz
3033 zurz
Name: stem, dtype: object]
我不明白的是,当我在其他列表上使用此功能时,它会起作用
test = ['abbbb','acccc','adddd']
plop = [x[-4:] for x in test]
['bbbb', 'cccc', 'dddd']
这应该有效:
X = data['stem'].apply(lambda x: x[-4:])
请试试这个,它对我有用。
li = ['abalanz', 'abander', 'abandon', 'best', 'bib']
df = pd.DataFrame(li, columns=['stem'])
df
stem
0 abalanz
1 abander
2 abandon
3 best
4 bib
df['stem'] = df['stem'].apply(lambda row: row[-4:])
df
stem
0 lanz
1 nder
2 ndon
3 best
4 bib
您可以将列(系列)转换为字符串并进行切片
X = data['stem'].str[-4:]
结果:
0 lanz
1 nder
2 ndon
3 anic
4 arat
5 zurz
我有一系列的单词,我只想保留最后四个字母:
X = data['stem']
0 abalanz
1 abander
2 abandon
3 abanic
4 abarat
3029 best
3030 bib
3031 bolb
3032 Laz
3033 zurz
我尝试了str.replace但是对系列没影响
X = pd.Series(X).str.replace('[-4:]', '', regex=False)
我尝试将系列转换为列表,然后对其进行操作,但这只保留了列表的最后四项。
test = [X]
plop = [x[-4:] for x in test]
[3030 bib
3031 bolb
3032 Laz
3033 zurz
Name: stem, dtype: object]
我不明白的是,当我在其他列表上使用此功能时,它会起作用
test = ['abbbb','acccc','adddd']
plop = [x[-4:] for x in test]
['bbbb', 'cccc', 'dddd']
这应该有效:
X = data['stem'].apply(lambda x: x[-4:])
请试试这个,它对我有用。
li = ['abalanz', 'abander', 'abandon', 'best', 'bib']
df = pd.DataFrame(li, columns=['stem'])
df
stem
0 abalanz
1 abander
2 abandon
3 best
4 bib
df['stem'] = df['stem'].apply(lambda row: row[-4:])
df
stem
0 lanz
1 nder
2 ndon
3 best
4 bib
您可以将列(系列)转换为字符串并进行切片
X = data['stem'].str[-4:]
结果:
0 lanz
1 nder
2 ndon
3 anic
4 arat
5 zurz