8

I am making a python code that first averages minute data into hourly data. I then want to multiply the values in the two columns in the hourly data and create a new column with the multiplied values. I am stuck on the multiplication step.

import pandas as pd
import numpy as np

df = pd.read_csv("inputfile.csv", index_col="DateTime", parse_dates=True)
df = df.resample('1H').mean()
df = df.reindex(pd.date_range(df.index.min(), df.index.max(), freq="1H"))
df.to_csv('outputfile.csv', index=True, index_label="DateTime")

Data from hourly averages

DateTime        current     voltage
11/1/2014 0:00  3.366184207 12.1758535
11/1/2014 1:00  3.361604775 12.1827364
11/1/2014 2:00  3.358049691 12.17596822
11/1/2014 3:00  3.354833198 12.1827364
11/1/2014 4:00  3.361096907 12.1827364
11/1/2014 5:00  3.361096907 12.1827364
11/1/2014 6:00  3.366344918 15.72258904
11/1/2014 7:00  3.419681019 1495.925115
11/1/2014 8:00  3.663316184 1870.538086
11/1/2014 9:00  4.369056237 1925.408667
11/1/2014 10:00 4.404945809 1938.888254
11/1/2014 11:00 4.711192238 1994.759897
11/1/2014 12:00 4.82263279  1995.281601
11/1/2014 13:00 4.428242773 1961.089536
11/1/2014 14:00 4.038091129 1895.686707
11/1/2014 15:00 4.04098199  1904.352924
11/1/2014 16:00 3.748518044 1852.646768
11/1/2014 17:00 3.397967499 1554.434254
11/1/2014 18:00 3.371380174 56.24243593
11/1/2014 19:00 3.375613815 12.18733199
11/1/2014 20:00 3.369686692 12.18239812
11/1/2014 21:00 3.367993271 12.18351949
11/1/2014 22:00 3.374089682 12.17048603
11/1/2014 23:00 3.367485231 12.18946266

I would like to multiply the current column by voltage column and create a new column with those values.

1
  • 1
    did you try df['current'] * df['voltage']? Commented Feb 24, 2017 at 16:47

2 Answers 2

11

You can try something like this:

df['Power'] = df['current']*df['voltage']
Sign up to request clarification or add additional context in comments.

8 Comments

this works however is there anyway to write this without typing in the column header and just use the column number to multiply.
@acb what do you mean?
sorry kind of confusing to word by my actual data has about 10 columns for current. I would like to multiply each column by the voltage and create 10 new columns for power.
@acb do you have multiple columns named 'current'?
@BFurtado just do df.loc[:, 'Power'] = ... instead.
|
8

df[newcolumn] = df['current']*df['voltage']
will work.
You can name provide newcolumn as a variable.

def getPower(df, newColumn, numOfCol):
    for i in range(numOfCol):
        current = 'current#%d' % (i+1)
        voltage = 'voltage#%d' % (i+1)
        power   = 'power#%d' % (i+1)
    df[power] = df[current]*df[voltage]

getPower(df, 'Power', numOfCols) would create the column.

EDIT: This will work if you named your current columns like 'current1', current2',...

2 Comments

Just saw this. Could you help me understand what #%d is and what can is the 'current#%d' % (i+1) step doing please?
%d (%s for string ) is to mark the place you want to insert a decimal value in a string. The current method is "sometring {}".format(obj_to_insert)

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.