从不同长度的字符串值中提取某些整数,其中包含不需要的整数。模式或位置
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"
当我想 find/fill StartStatNum 和我没有的电台 ID 号码时,问题就会出现。
我一直在努力去了解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+))")
我也试过 here
中的以下方法
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
现在我们可以通过在删除NaN
行的df上调用地图来填充缺少的站号,并将站名设置为索引,这将查找站名和[=20= 】站号:
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
我有点像初级程序员,正在寻求帮助和问题的解释。我希望将 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"
当我想 find/fill StartStatNum 和我没有的电台 ID 号码时,问题就会出现。
我一直在努力去了解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+))")
我也试过 here
中的以下方法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
现在我们可以通过在删除NaN
行的df上调用地图来填充缺少的站号,并将站名设置为索引,这将查找站名和[=20= 】站号:
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