Home > other >  Replace all negative numbers in a csv file with 0
Replace all negative numbers in a csv file with 0

Time:07-18

i was looking for guidance on how to read a csv file and replace all negative numbers with a 0.

My attempt so far I have used a regular expression to try and defina all negative numbers then use the replace function to replace with 0. Although it has not worked yet.

Here is my code any guidance would be appreciated. Thanks

text = open("input.csv", "r")
text = ''.join([i for i in text]) \
    .replace("^-\d $", "0")
x = open("output.csv","w")
x.writelines(text)
x.close()

CodePudding user response:

Why don't you use pandas?

create Dataframe. For example

import pandas as pd
df = pd.DataFrame({'a': [0, -1, 2], 'b': [-3, 2, 1],
                           'c': ['foo', 'goo', 'bar']})
# pd.read_csv('data.csv')

Ofcourse this needs to be done with pandas.read_csv read more here

Your DataFrame should look like this:

   a  b    c
0  0 -3  foo
1 -1  2  goo
2  2  1  bar

Then replace all negative numbers

num = df._get_numeric_data()
num[num < 0] = 0

Now your DataFrame should look like that

   a  b    c
0  0  0  foo
1  0  2  goo
2  2  1  bar

After that you can save your DataFrame as a .csv file.

df.to_csv('data.csv', header=False)  # or header=True

This will override the data.csv file.

CodePudding user response:

As i know, string replace method in python doesn't work with regexs, but it replaces substrings to another ones. Try python's re.sub.

  • Related