[英]Extract certain integers from string value, of different length, which contains unwanted integers. Pattern or Position
我有些是初学者程序员,正在寻求帮助和问题的解释。 我正在寻找将ID编号从字符串中提取到新列中,然后填写缺失的编号。
我正在使用pandas数据框,并且具有以下街道名称集,其中一些具有ID号,而另一些则缺失:
*Start station*:
"19th & L St (31224)"
"14th & R St NW (31202)"
"Paul Rd & Pl NW (31602)"
"14th & R St NW"
"19th & L St"
"Paul Rd & Pl NW"
My desired outcome:
*Start station* *StartStatNum*
"14th & R St NW" 31202
"19th & L St" 31224
"Paul Rd & Pl NW" 31602
"14th & R St NW" 31202
"19th & L St" 31224
"Paul Rd & Pl NW" 31602
拆分的第一步后,我遇到了困难。 我可以根据位置使用以下内容进行拆分:
def Stat_Num(Stat_Num):
return Stat_Num.split('(')[-1].split(')')[0].strip()
db["StartStatNum"] = pd.DataFrame({'Num':db['Start station'].apply(Stat_Num)})
But this gives:
*Start station* *StartStatNum*
"19th & L St (31224)" 31202
"14th & R St NW (31202)" 31224
"Paul Rd & Pl NW (31602)" 31602
"14th & R St NW" "14th & R St NW"
"19th & L St" "19th & L St"
"Paul Rd & Pl NW" "Paul Rd & Pl NW"
然后,当我想用我没有的工作站ID号查找/填充StartStatNum时,就会出现问题。
我一直在尝试了解str.extract, str.contains, re.findall
并尝试了以下方法作为可能的垫脚石:
db['Start_S2'] = db['Start_Stat_Num'].str.extract(" ((\d+))")
db['Start_S2'] = db['Start station'].str.contains(" ((\d+))")
db['Start_S2'] = db['Start station'].re.findall(" ((\d+))")
我也从这里尝试了以下
def parseIntegers(mixedList):
return [x for x in db['Start station'] if (isinstance(x, int) or isinstance(x, long)) and not isinstance(x, bool)]
但是,当我传入值时,会得到带有1个值的列表'x'。 有点菜鸟,我认为走模式路线不是最好的,因为它也会吸收不需要的整数(尽管我可能会求助于Nan,因为它们会小于30000(ID号的最小值))我也有一个想法,那就是我可能忽略了一些简单的事情,但是经过连续20个小时的搜索和大量搜索之后,我有些茫然。
任何帮助都将非常有帮助。
解决方案可能是通过转换创建数据框
station -> id
喜欢
l = ["19th & L St (31224)",
"14th & R St NW (31202)",
"Paul Rd & Pl NW (31602)",
"14th & R St NW",
"19th & L St",
"Paul Rd & Pl NW",]
df = pd.DataFrame( {"station":l})
df_dict = df['station'].str.extract("(?P<station_name>.*)\((?P<id>\d+)\)").dropna()
print df_dict
# result:
station_name id
0 19th & L St 31224
1 14th & R St NW 31202
2 Paul Rd & Pl NW 31602
[3 rows x 2 columns]
从那里开始,您可以使用一些列表理解:
l2 = [ [row["station_name"], row["id"]]
for line in l
for k,row in df_dict.iterrows()
if row["station_name"].strip() in line]
要得到:
[['19th & L St ', '31224'],
['14th & R St NW ', '31202'],
['Paul Rd & Pl NW ', '31602'],
['14th & R St NW ', '31202'],
['19th & L St ', '31224'],
['Paul Rd & Pl NW ', '31602']]
我让你在数据框中转换后面的内容...
至少在最后一部分可能会有更好的解决方案...
这是一种对我有用的方法,首先提取括号中的数字:
In [71]:
df['start stat num'] = df['Start station'].str.findall(r'\((\d+)\)').str[0]
df
Out[71]:
Start station start stat num
0 19th & L St (31224) 31224
1 14th & R St NW (31202) 31202
2 Paul Rd & Pl NW (31602) 31602
3 14th & R St NW NaN
4 19th & L St NaN
5 Paul Rd & Pl NW NaN
现在删除号码,因为我们不再需要它了:
In [72]:
df['Start station'] = df['Start station'].str.split(' \(').str[0]
df
Out[72]:
Start station start stat num
0 19th & L St 31224
1 14th & R St NW 31202
2 Paul Rd & Pl NW 31602
3 14th & R St NW NaN
4 19th & L St NaN
5 Paul Rd & Pl NW NaN
现在,我们可以通过在df上调用map并删除NaN
行,然后将站名设置为索引来填充缺少的站号,这将查找站名并返回站号:
In [73]:
df['start stat num'] = df['Start station'].map(df.dropna().set_index('Start station')['start stat num'])
df
Out[73]:
Start station start stat num
0 19th & L St 31224
1 14th & R St NW 31202
2 Paul Rd & Pl NW 31602
3 14th & R St NW 31202
4 19th & L St 31224
5 Paul Rd & Pl NW 31602
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.