如何解决为什么我通过在代码中以不同的顺序放置“年”和“年”来获得不同的输出
我所做的只是将“年份”和“年份”的位置从第一行切换到第二行,反之亦然..
这是原来的专栏
10+ years 653
< 1 year 249
2 years 243
3 years 235
5 years 202
4 years 191
1 year 177
6 years 163
7 years 127
8 years 108
9 years 72
. 2
Name: Employment.Length,dtype: int64
第一个例子(第一行'年',第二行'年')
raw_data['Employment.Length'] = raw_data['Employment.Length'].str.replace('years',' ')
raw_data['Employment.Length'] = raw_data['Employment.Length'].str.replace('year',' ')
raw_data['Employment.Length'] = np.where(raw_data['Employment.Length'].str[:2]=='10',10,raw_data['Employment.Length'])
raw_data['Employment.Length'] = np.where(raw_data['Employment.Length'].str[0]=='<',raw_data['Employment.Length'])
raw_data['Employment.Length'] = pd.to_numeric(raw_data['Employment.Length'],errors = 'coerce')
输出
10.0 653
0.0 249
2.0 243
3.0 235
5.0 202
4.0 191
1.0 177
6.0 163
7.0 127
8.0 108
9.0 72
Name: Employment.Length,dtype: int64
第二个例子(第一行'year',第二行'years')
raw_data_copy['Employment.Length'] = raw_data_copy['Employment.Length'].str.replace('year',' ')
raw_data_copy['Employment.Length'] = raw_data_copy['Employment.Length'].str.replace('years',' ')
raw_data_copy['Employment.Length'] = np.where(raw_data_copy['Employment.Length'].str[:2]=='10',raw_data_copy['Employment.Length'])
raw_data_copy['Employment.Length'] = np.where(raw_data_copy['Employment.Length'].str[0]=='<',raw_data_copy['Employment.Length'])
raw_data_copy['Employment.Length'] = pd.to_numeric(raw_data_copy['Employment.Length'],errors = 'coerce')
输出
10.0 653
0.0 249
1.0 177
Name: Employment.Length,dtype: int64
还有一件事是,当我用“年份”注释掉我的第二行时,它给了我与第一个示例相同的输出。 当我用'years'注释掉我的第二行时,它给了我与第二个例子相同的输出。
第三个例子
raw_data_copy['Employment.Length'] = raw_data_copy['Employment.Length'].str.replace('years',' ')
#raw_data_copy['Employment.Length'] = raw_data_copy['Employment.Length'].str.replace('years',' ')
raw_data_copy['Employment.Length'] = np.where(raw_data_copy['Employment.Length'].str[:2]=='10',raw_data_copy['Employment.Length'])
raw_data_copy['Employment.Length'] = np.where(raw_data_copy['Employment.Length'].str[0]=='<',raw_data_copy['Employment.Length'])
raw_data_copy['Employment.Length'] = pd.to_numeric(raw_data_copy['Employment.Length'],errors = 'coerce')
输出
10.0 653
0.0 249
2.0 243
3.0 235
5.0 202
4.0 191
6.0 163
7.0 127
8.0 108
9.0 72
Name: Employment.Length,dtype: int64
解决方法
如果您首先将 'year'
替换为 ' '
,则 'years'
变为 ' s'
,并且 's'
不再被您后续的 str.replace('years',' ')
替换.
使用带有可选 s
的替换替换多个后续替换:'year[s]?'
import pandas as pd
s = pd.Series(['year','years','foo'])
s.str.replace('year[s]?',' ')
#0
#1
#2 foo
#dtype: object
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。