Home > Back-end >  How to edit data in CSV with equations to convert to new values?
How to edit data in CSV with equations to convert to new values?

Time:02-23

I've been trying to find a solution. But most of all, I found only a few lines of csv editing. But the data that I have and have to solve consists of thousands of lines. 

The data that I use is the value from the sensor-read. but it comes out in voltage. I need to convert it by using equations (which I already have).But I don't know how to use it to edit with many row in csv.

Data sample

ID Voltage
1 1979
2 1978
... ...
49999 1976
50000 1976

I want to create new column and convert value from old existing column to new column and export to new csv file. Is there any way that can solve it?

I don't know if I should give an example of the equation. But let's just put it together. My equations will be like

Moisture = 100 - ( 0.01 x Voltage )

Here is sample of csv that I want.

ID Voltage Moisture
1 1979 80
2 1978 80
... ... ...
49999 1976 81
50000 1976 81

CodePudding user response:

load the csv into a dataframe, this is done easily with pandas

import pandas as pd

df = pd.read_csv('file_path.csv')

then you can create a new column and assign it a value base of existing columns

df['Moisture'] = 100 - (df['Voltage'] * 0.01)

then you can save the dataframe back to a csv

df.to_csv('file_path.csv', index=False)

CodePudding user response:

You can use csv module for that. You can read data from csv into dictionaries and then just update those dictionaries and write it back. Something like this:

import csv

old_data = list()
with open('data.csv', newline='') as f:
    old_data = list(csv.DictReader(f))
    for d in csv_reader:
        d['Moisture'] = ''

with open('new_data.csv', mode='w') as csv_file:
    writer = csv.DictWriter(csv_file,
                                    fieldnames=['ID', 'Voltage', 'Moisture'],
                                    lineterminator='\n')
            writer.writeheader()
            writer.writerows(old_data)

CodePudding user response:

Did you have a look at the pandas library ? It allows you to manipulate any kind of data.

Here is an example for your case:

import pandas as pd

# Reading your csv file:
df = pd.read_csv("your_csv_file.csv", index="ID")

# Creating a new column:
df["Moisture"] = 100 - (0.01 * df["Voltage"])

# Exporting new csv:
df.to_csv("new_csv_with_moisture.csv")

  • Related