How do I write regular expression data from column data to a new column?

Asked 2 years ago, Updated 2 years ago, 45 views

In the following data frame df,

a
 1 abc def
 2123 567  3qqqeeee

Add regular expression data to column a and add it to the new column

ab
 1 abc def def def
 2123 567 567  3qqqeeeeeeeeeeeeeeee>

What should I do to ?

df['b'] = Rejected by regular expression function (df['a']).

python pandas

2022-09-30 21:23

1 Answers

We truncated the value in str.extract for Series a and substituted the result in a new column b.

>>>df=pd.DataFrame([[abc def], ['123567'], ['qqqeeee']], columns=['a'])
>>df
         a
0abc def
1  123 567
2qqqeeeee
>>>df['a'].str.extract(r'(.{3}$',expand=True)
     0
0def
1  567
2eee
>>>df['b'] = df['a'].str.extract(r'(.{3})$',expand=False)
>>df
         ab
0abc def def def
1  123 567  567
2qqqeeeeeeeee


2022-09-30 21:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.