0

I have checked many solutions but I have been unable to apply any to my problem.

I have a .csv file, like this:

    Header_A;Header_B
    0;1
    1;4
    5;6
    6;7
    9;8

Now I want to pythonically add another column "Header_C" to it and calculate its values (x) from the addition from the first 2 columns per definition, so something like

    def add(a, b):
        x = a + b
        return x

where x will be the value of column Header_C and a, b are the sums of columns Header_A and Header_B.

The result should look like this:

    Header_A;Header_B;Header_C
    0;1;1
    1;4;5
    5;6;11
    6;7;13
    9;8;17

If possible without installing additional modules. Output can be a new .csv file.

Thanks a lot!

2 Answers 2

1

pandas is your solution:

import pandas as pd

df = pd.read_csv('a.csv')
df['Header_C'] = df['Header_A'] + df['Header_B']

df.to_csv('b.csv', sep=';', index=False)

For more info on pandas please visit http://pandas.pydata.org/

Sign up to request clarification or add additional context in comments.

3 Comments

I came across Pandas, but I was hoping to do it without installing any modules. Maybe the CSV module or so, but I can't find a solution for my problem in the documentation or elsewhere
I tried your example again and I got this error:Traceback (most recent call last): File "<string>", line 254, in run_nodebug File "D:\Dropbox\Python Projects\CSV\CSV mit Pandas.py", line 5, in <module> df["Header_C"] = df["Header_A"] + df["Header_B"] ... KeyError: 'Header_A'
I found the cause. Just apply this change: df = pd.read_csv('a.csv', sep=';')
0

I still got the same error even with the line

    df = pd.read_csv('a.csv', sep=';')

But you inspired me and got me the idea that the problem might be header! So I tried some things and now actually got it working. Here is the fully working code:

import pandas
df = pandas.read_csv("a.csv", sep=';', names=['Header_A', 'Header_B'], header=0)
df['Header_C'] = df["Header_A"] + df["Header_B"]
df.to_csv("b.csv", sep=';', index=False)

if header is set to NONE, Python treats the values as strings, which would result in stuff like this:

9 + 3 = 93

If you set header=0, you override that. I am not sure if my explanation is accurate, but now the program does what I want! Thanks a lot!

However, I am still interested in a solution with the CSV module or purely Python WITHOUT module! Anyone?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.