Search code examples
stringpython-3.xcsvline

Removing words from csv-file's line in python3


How do you read a csv-file and remove a part from it, leaving the rest of the line intact?

If I have a line like this:

Chicago,0.6,5.5,1.3,11.1,NewYork,0.0

And my objective is to end up with this:

0.6,5.5,1.3,11.1,0.0

How should i do it? Is it possible to do it so that only numbers would be taken into account?


Solution

  • Might not be very elegant solution, but you can try this:

    import pandas as pd
    
    def isnumber(s):
        """Return True if it is a number, otherwise return False."""
        try:
            float(s)
            return True
        except ValueError:
            return False
    
    df = pd.read_csv('/PATH/TO/FILE.csv', sep=',', header=None)
    df = df[df.applymap(isnumber)].dropna(axis='columns')
    
    df.to_csv('PATH/TO/OUTPUTFILE.csv', sep=',', header=None)
    

    An important note: if you have more lines in your file, dropna will remove all columns with non-numbers.