Python 如果正则表达式不适合,则在extract()之后保留值。熊猫
我的意见:Python 如果正则表达式不适合,则在extract()之后保留值。熊猫,python,pandas,Python,Pandas,我的意见: df=pd.DataFrame({'A':['adam','monica','joe doe','michael mo'], 'B':['david','valenti',np.nan,np.nan]}) print(df) A B 0 adam david 1 monica valenti 2 joe doe NaN 3 michael mo NaN 我需要将空格后的字符串提取到第
df=pd.DataFrame({'A':['adam','monica','joe doe','michael mo'], 'B':['david','valenti',np.nan,np.nan]})
print(df)
A B
0 adam david
1 monica valenti
2 joe doe NaN
3 michael mo NaN
我需要将空格后的字符串提取到第二列,但在使用代码时:
df['B'] = df['A'].str.extract(r'( [a-zA-Z](.*))')
print(df)
A B
0 adam NaN
1 monica NaN
2 joe doe doe
3 michael mo mo
我在未提取值的每个单元格中接收到NaN
。如何避免呢?
我尝试使用以下代码仅从存在NaN的行中提取:
df.loc[df.B.isna(),'B'] = df.loc[df.B.isna(),'A'].str.extract(r'( [a-zA-Z](.*))')
ValueError: Incompatible indexer with DataFrame
预期产出:
A B
0 adam david
1 monica valenti
2 joe doe doe
3 michael mo mo
我认为解决方案应该简化-按空格分割,得到第二个列表并传递给函数:
df['B'] = df['B'].fillna(df['A'].str.split().str[1])
print (df)
A B
0 adam david
1 monica valenti
2 joe doe doe
3 michael mo mo
详细信息:
print (df['A'].str.split().str[1])
0 NaN
1 NaN
2 doe
3 mo
Name: A, dtype: object
print (df['A'].str.extract(r'( [a-zA-Z].*)', expand=False))
0 NaN
1 NaN
2 doe
3 mo
Name: A, dtype: object
应更改您的解决方案:
df['B'] = df['A'].str.extract(r'( [a-zA-Z](.*))')[0].fillna(df.B)
print (df)
A B
0 adam david
1 monica valenti
2 joe doe doe
3 michael mo mo
对于系列
,更好的解决方案是更改regex和expand=False
:
df['B'] = df['A'].str.extract(r'( [a-zA-Z].*)', expand=False).fillna(df.B)
print (df)
A B
0 adam david
1 monica valenti
2 joe doe doe
3 michael mo mo
详细信息:
print (df['A'].str.split().str[1])
0 NaN
1 NaN
2 doe
3 mo
Name: A, dtype: object
print (df['A'].str.extract(r'( [a-zA-Z].*)', expand=False))
0 NaN
1 NaN
2 doe
3 mo
Name: A, dtype: object
编辑:
对于从第一列中提取值,最简单的方法是使用:
df1 = df['A'].str.split(expand=True)
df['A'] = df1[0]
df['B'] = df['B'].fillna(df1[1])
print (df)
A B
0 adam david
1 monica valenti
2 joe doe
3 michael mo
我认为解决方案应该简化-按空格分割,得到第二个列表并传递给函数:
df['B'] = df['B'].fillna(df['A'].str.split().str[1])
print (df)
A B
0 adam david
1 monica valenti
2 joe doe doe
3 michael mo mo
详细信息:
print (df['A'].str.split().str[1])
0 NaN
1 NaN
2 doe
3 mo
Name: A, dtype: object
print (df['A'].str.extract(r'( [a-zA-Z].*)', expand=False))
0 NaN
1 NaN
2 doe
3 mo
Name: A, dtype: object
应更改您的解决方案:
df['B'] = df['A'].str.extract(r'( [a-zA-Z](.*))')[0].fillna(df.B)
print (df)
A B
0 adam david
1 monica valenti
2 joe doe doe
3 michael mo mo
对于系列
,更好的解决方案是更改regex和expand=False
:
df['B'] = df['A'].str.extract(r'( [a-zA-Z].*)', expand=False).fillna(df.B)
print (df)
A B
0 adam david
1 monica valenti
2 joe doe doe
3 michael mo mo
详细信息:
print (df['A'].str.split().str[1])
0 NaN
1 NaN
2 doe
3 mo
Name: A, dtype: object
print (df['A'].str.extract(r'( [a-zA-Z].*)', expand=False))
0 NaN
1 NaN
2 doe
3 mo
Name: A, dtype: object
编辑:
对于从第一列中提取值,最简单的方法是使用:
df1 = df['A'].str.split(expand=True)
df['A'] = df1[0]
df['B'] = df['B'].fillna(df1[1])
print (df)
A B
0 adam david
1 monica valenti
2 joe doe
3 michael mo
你的方法不起作用,因为你陈述的左右两边形状不同。左侧部分具有形状
(2,)
,右侧部分具有形状(2,2)
:
返回:
2 NaN
3 NaN
0 1
2 doe oe
3 mo o
2 oe
3 o
你想在这里填上:
df.loc[df.B.isna(),'A'].str.extract(r'( [a-zA-Z](.*))')
返回:
2 NaN
3 NaN
0 1
2 doe oe
3 mo o
2 oe
3 o
您可以选择列1
,然后它将具有与左侧部分相同的形状(2,)
,并适合:
df.loc[df.B.isna(),'A'].str.extract(r'( [a-zA-Z](.*))')[1]
返回:
2 NaN
3 NaN
0 1
2 doe oe
3 mo o
2 oe
3 o
你的方法不起作用,因为你陈述的左右两边形状不同。左侧部分具有形状
(2,)
,右侧部分具有形状(2,2)
:
返回:
2 NaN
3 NaN
0 1
2 doe oe
3 mo o
2 oe
3 o
你想在这里填上:
df.loc[df.B.isna(),'A'].str.extract(r'( [a-zA-Z](.*))')
返回:
2 NaN
3 NaN
0 1
2 doe oe
3 mo o
2 oe
3 o
您可以选择列1
,然后它将具有与左侧部分相同的形状(2,)
,并适合:
df.loc[df.B.isna(),'A'].str.extract(r'( [a-zA-Z](.*))')[1]
返回:
2 NaN
3 NaN
0 1
2 doe oe
3 mo o
2 oe
3 o
谢谢,一如既往!我能问一下从列A中删除提取字符串的最快方法是什么吗?我应该使用类似于
df['A']=df.apply(lambda x:x['A'].replace(x['B'],“”)
?@sygneto-是的,这是可能的解决方案,或者您可以在之后使用helper DataFrame赋值。split()!我能问一下从列A中删除提取字符串的最快方法是什么吗?我应该使用类似于df['A']=df.apply(lambda x:x['A'].replace(x['B'],“”)
?@sygneto-是的,这是可能的解决方案,或者您可以在之后使用helper DataFrame赋值。split()
-添加了解答。