Reputation: 196
I have this Dataframe: I want to replace the 1 with df.rule1 and 2 with df.rule2
+---+---------+------+
|SNo|Operation|Points|
+---+---------+------+
| 1| 1 & 2| 100|
| 2| 1 | 2| 200|
| 3|1 | 2 & 3| 350|
+---+---------+------+
I want this dataframe into this:
+---+------------------------------+------+
|SNo|Operation |Points|
+---+------------------------------+------+
|1 |df.rule1 & df.rule2 |100 |
|2 |df.rule1 | df.rule2 |200 |
|3 |df.rule1 | df.rule2 & df.rule3|350 |
+---+------------------------------+------+
Upvotes: 1
Views: 63
Reputation: 19895
Assuming that is a pyspark
DataFrame
, we can use regexp_replace
:
from pyspark.sql import functions as F
df.withColumn('Operation', F.regexp_replace('Operation', r'\d', r'df.rule\1'))
Upvotes: 2
Reputation: 29742
Use pd.Series.replace
with regex=True
:
df['Operation'].replace('(\d)', 'df.rule\\1', regex=True)
Output:
0 df.rule1 & df.rule2
1 df.rule1 | df.rule2
2 df.rule1 | df.rule2 & df.rule3
Name: Operation, dtype: object
Upvotes: 1