2017-02-24 948 views
1

我正在製作一個python代碼,它首先將分鐘數據平均到小時數據中。然後,我想在小時數據中乘以兩列中的值,並用乘數值創建一個新列。我被困在乘法步驟。從小時圖均線Python代碼將兩列相乘,然後用值創建新列

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 

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") 

數據我想乘以電壓列在當前列,並創建這些值的新列。

+1

當前的列你嘗試'DF [ '目前'] * DF [ '電壓']'? – EdChum

回答

3

df[newcolumn] = df['current']*df['voltage']
會工作。
您可以將newcolumn命名爲變量。

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. 

編輯:這將工作,如果你命名爲喜歡'current1', current2',...

3

你可以嘗試這樣的事情:

df['Power'] = df['current']*df['voltage'] 
+0

這個工作,但是無論如何寫這個沒有在列標題中鍵入,只是使用列數乘。 – acb

+0

@acb你是什麼意思? – AsheKetchum

+0

對不起,我的實際數據有點混淆詞,目前大約有10列。我想將每列乘以電壓,並創建10個新的電源列。 – acb

相關問題