Dataframe split before a specific string for all rows

J1701

I have a dataframe (df) that contains 30 000 rows coming from a web scraping exercice

Name     NameID                                                            Age

John     www.link.com/www.link.com/https://www.link.com/ct/John             25
Samanta  www.link.com/www.link.com/https://www.link.com/ct/Samanta          24
Johnny   www.link.com/www.link.com/                                         22
Mary     www.link.com/www.link.com/https://www.link.com/ct/Mary             35

I want to clean the "NameID" row in a way where i only read "https://www.link.com/ct/ " part. So my output dataframe should look like this :

 Name     NameID                                  Age

John     https://www.link.com/ct/John             25
Samanta  https://www.link.com/ct/Samanta          24
Johnny                                            22
Mary     https://www.link.com/ct/Mary             35

My code so far:

df['NameID'] = df['NameID'].str.split("https://www.link.com/ct/")[1][1]
df['NameID'] =  "https://www.link.com/ct/" + df['NameID'].astype(str)

The output looks like this now:

Name     NameID                                  Age

John     https://www.link.com/ct/John             25
Samanta  https://www.link.com/ct/John             24
Johnny   https://www.link.com/ct/John             22
Mary     https://www.link.com/ct/John             35

Any help?

sophocles

You're close, you need .str[1]. Try changing your code to this:

df['NameID'] = df['NameID'].str.split("https://www.link.com/ct/").str[1]
df['NameID'] =  "https://www.link.com/ct/" + df['NameID'].astype(str)

df

      Name                           NameID  Age
0     John     https://www.link.com/ct/John   25
1  Samanta  https://www.link.com/ct/Samanta   24
2   Johnny      https://www.link.com/ct/nan   22
3     Mary     https://www.link.com/ct/Mary   35

You can tweak your code a bit to return back a '', as you specified in your desired outcome.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dataframe split before a specific string for all rows

From Dev

how to split a dataframe by specific rows in r

From Dev

Ruby - How to split a string before specific keywords

From Dev

Divide all rows in a pandas dataframe by a specific row

From Dev

Python String split using comma before a specific string

From Dev

Get all the rows raleted with a specific string

From Dev

Convert list to string of all dataframe rows

From Dev

How do I get all the rows before a specific index in Pandas?

From Dev

How to loop through Pandas DataFrame and split a string into multiple rows

From Dev

Python: Count instances of a specific character in all rows within a dataframe column

From Dev

Filtering all rows based on a specific value in Pandas dataframe

From Dev

In python, how to shift and fill with a specific values for all the shifted rows in DataFrame?

From Dev

How to add a column to a dataframe and set all rows to a specific value

From Dev

Split string before regex

From Dev

Split string after : and before?

From Dev

Split string before regex

From Dev

Split String in small specific length parts and return all those parts

From Dev

Remove all leading string before two specific letters in R

From Dev

How to remove all characters from a string before a specific character

From Dev

Delete all lines before first occurrence of specific string in file

From Dev

Remove all leading string before two specific letters in R

From Dev

Java regex - Extract all float numbers before specific unit in String

From Dev

String split into duplicate rows

From Dev

How to split a [String] into rows?

From Dev

split string by rows

From Dev

How to split a [String] into rows?

From Dev

pandas replace specific string with numeric value in a new column for all rows

From Dev

python: remove all rows in pandas dataframe that contain a string

From Dev

python: remove all rows in pandas dataframe that contain a string

Related Related

  1. 1

    Dataframe split before a specific string for all rows

  2. 2

    how to split a dataframe by specific rows in r

  3. 3

    Ruby - How to split a string before specific keywords

  4. 4

    Divide all rows in a pandas dataframe by a specific row

  5. 5

    Python String split using comma before a specific string

  6. 6

    Get all the rows raleted with a specific string

  7. 7

    Convert list to string of all dataframe rows

  8. 8

    How do I get all the rows before a specific index in Pandas?

  9. 9

    How to loop through Pandas DataFrame and split a string into multiple rows

  10. 10

    Python: Count instances of a specific character in all rows within a dataframe column

  11. 11

    Filtering all rows based on a specific value in Pandas dataframe

  12. 12

    In python, how to shift and fill with a specific values for all the shifted rows in DataFrame?

  13. 13

    How to add a column to a dataframe and set all rows to a specific value

  14. 14

    Split string before regex

  15. 15

    Split string after : and before?

  16. 16

    Split string before regex

  17. 17

    Split String in small specific length parts and return all those parts

  18. 18

    Remove all leading string before two specific letters in R

  19. 19

    How to remove all characters from a string before a specific character

  20. 20

    Delete all lines before first occurrence of specific string in file

  21. 21

    Remove all leading string before two specific letters in R

  22. 22

    Java regex - Extract all float numbers before specific unit in String

  23. 23

    String split into duplicate rows

  24. 24

    How to split a [String] into rows?

  25. 25

    split string by rows

  26. 26

    How to split a [String] into rows?

  27. 27

    pandas replace specific string with numeric value in a new column for all rows

  28. 28

    python: remove all rows in pandas dataframe that contain a string

  29. 29

    python: remove all rows in pandas dataframe that contain a string

HotTag

Archive