Remove trailing whitespaces from CSV

user2676336

I am having trouble passing the stripped whitespace for stop_headsign back to stop_times for output to CSV. Alternatively, is there a way to .rstrip() the entire stop_headsign column?

Here is the stop_times.txt Gist.

Here is the pandas rstrip reference.

Below is my code:

import pandas as pd

stop_times = pd.read_csv('csv/stop_times.txt')

for x in stop_times['stop_headsign']:
    if type(x) == str:
        x = x.rstrip()
        # figure out how to pass store new value
    if type(x) == float:
        pass

stop_times['distance'] = 0

stop_times.to_csv('csv/stop_times.csv', index=False)

Below is what the csv output shows:

trip_id,arrival_time,departure_time,stop_id,stop_sequence,pickup_type,drop_off_type,stop_headsign,distance
568036,,,00382,26,0,0,78 UO                                             ,0
568036,,,00396,7,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00398,8,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00400,9,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00404,10,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00407,11,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00412,13,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00413,14,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00416,15,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00418,16,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00419,17,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00422,18,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00423,19,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00425,20,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,00427,21,0,0,78 UO <> 78 via 18th AVE                          ,0
568036,,,01006,2,0,0,78 UO <> 78 via 18th AVE                          ,0
filmor

Pandas has a handy "extension" property on Series objects for this:

stop_times["stop_headsign"] = stop_times["stop_headsign"].str.rstrip()

Actually, your link is pointing to this, .str is of type StringMethods.

There is a section Vectorized String Methods in the basics-documentation on this that links to Working with Text Data.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I remove trailing certain blocks of whitespaces from a string in javascript?

From Dev

How to remove the Whitespaces from a link

From Dev

How to auto-remove trailing whitespaces on save in Matlab?

From Dev

Nano - highlight trailing whitespaces

From Dev

Remove whitespaces from speciifc part of file

From Dev

Remove whitespaces only from the right side of a String

From Dev

Remove Trailing Slash From the URL

From Dev

Remove trailing '/' from rails string

From Dev

remove trailing zeros from output

From Dev

Regex possible Whitespaces and trailing characters

From Dev

How to remove trailing blanks or linebreaks from CSV file created with write.table?

From Dev

How can I remove leading and trailing whitespace from all columns but one in a CSV?

From Dev

Remove whitespaces from XML file read from a zip file

From Dev

Remove whitespaces and line breaks from captured data with Php Dom Document

From Dev

How to remove unnecessary whitespaces from string, so there are no extra spaces in HTML

From Dev

Remove whitespaces from beginning and end of each line in a file in python

From Dev

How to remove whitespaces from the beginning and end of loop phrase

From Dev

How to remove whitespaces and linebreaks from specific parts of a string?

From Dev

remove whitespaces and commas when reading from a file in python

From Dev

Remove all whitespaces in string

From Java

Htaccess: add/remove trailing slash from URL

From Dev

Remove Trailing Slash from Magento URL

From Dev

How to remove trailing newline from message field

From Dev

Remove trailing numbers from string js regexp

From Dev

Remove trailing zeroes after decimal from double

From Dev

Swift - Remove Trailing Zeros From Double

From Dev

Remove leading and trailing zeros from a string

From Dev

Best way to remove trailing zeros from a Decimal

From Dev

Remove trailing substring from String in Java

Related Related

  1. 1

    How can I remove trailing certain blocks of whitespaces from a string in javascript?

  2. 2

    How to remove the Whitespaces from a link

  3. 3

    How to auto-remove trailing whitespaces on save in Matlab?

  4. 4

    Nano - highlight trailing whitespaces

  5. 5

    Remove whitespaces from speciifc part of file

  6. 6

    Remove whitespaces only from the right side of a String

  7. 7

    Remove Trailing Slash From the URL

  8. 8

    Remove trailing '/' from rails string

  9. 9

    remove trailing zeros from output

  10. 10

    Regex possible Whitespaces and trailing characters

  11. 11

    How to remove trailing blanks or linebreaks from CSV file created with write.table?

  12. 12

    How can I remove leading and trailing whitespace from all columns but one in a CSV?

  13. 13

    Remove whitespaces from XML file read from a zip file

  14. 14

    Remove whitespaces and line breaks from captured data with Php Dom Document

  15. 15

    How to remove unnecessary whitespaces from string, so there are no extra spaces in HTML

  16. 16

    Remove whitespaces from beginning and end of each line in a file in python

  17. 17

    How to remove whitespaces from the beginning and end of loop phrase

  18. 18

    How to remove whitespaces and linebreaks from specific parts of a string?

  19. 19

    remove whitespaces and commas when reading from a file in python

  20. 20

    Remove all whitespaces in string

  21. 21

    Htaccess: add/remove trailing slash from URL

  22. 22

    Remove Trailing Slash from Magento URL

  23. 23

    How to remove trailing newline from message field

  24. 24

    Remove trailing numbers from string js regexp

  25. 25

    Remove trailing zeroes after decimal from double

  26. 26

    Swift - Remove Trailing Zeros From Double

  27. 27

    Remove leading and trailing zeros from a string

  28. 28

    Best way to remove trailing zeros from a Decimal

  29. 29

    Remove trailing substring from String in Java

HotTag

Archive