helpme
helpme

Reputation: 203

How to create a column that starts with and end with string value in another column?

How do I create a column that starts from "\"" and ends in "]" in another column?

For example

A                  new_column

\\loc\ggg.x]ddj    \\loc\ggg.x]
+\\lol\lll.d]aaa   \\lol\lll.d]

I tried doing this

df['new_column'] = df['A'].str.split(']').str[0]

but it included unneeded text and want to only start at X (\) and end with Y ("]").

Upvotes: 2

Views: 221

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195543

Try .str.extract:

df["new_column"] = df["A"].str.extract(r"(\\.*?\])")
print(df)

Prints:

                                                                                       A                 new_column
0                                                                        \\loc\ggg.x]ddj               \\loc\ggg.x]
1                                                                       +\\lol\lll.d]aaa               \\lol\lll.d]
2  \\ddf\gdd\Ps\s\3\s[a.xls]ss'!e+'\\d\\P\2\d[d.xls]Canjet'!B42+'\\df\gds\+'\\s\P[s.pdf]  \\ddf\gdd\Ps\s\3\s[a.xls]

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522094

You could use str.replace here with a capture group:

df["new_column"] = df["A"].str.replace(r'^.*?(\\\\.*\]).*$', r'\1')

Upvotes: 0

Related Questions