I want to subtract the present value by the previous value in each line and whenever there is N/A, it will copy the previous available value and subtract this by the previous available value.
When I run the codes below, I get the following message: ‘DataFrame’ object has no attribute ‘value’. Could anyone please help fix it?
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
df = pd.read_excel('ccy_test.xlsx')
X = df.iloc[3:, 1:]
df.fillna(method='pad')
count_row = df.shape[0]
count_col = df.shape[1]
z = df.value[:count_row,1:count_col] - df.value[:count_row,:count_col-1]
dz = pd.DataFrame(z)
There are some issues with the code you posted. The example file is a csv
file, so you need to refer to “ccy_test.csv”. The column Value
only contains 0’s, so for this example I use the column Open
.
Furthermore, I added to your read_csv
:
index_col=0
-> makes the first columndates
the indexparse_dates=[0]
-> parse the dates as dates (instead of strings)skiprows=3
-> don’t read the first rows, as they are not part of the tableheader=0
-> read the first row (after the skip) as column names
So:
import pandas as pd
df = pd.read_csv('ccy_test.csv', index_col=0, parse_dates=[0], skiprows=3, header=0)
df = df.fillna(method='pad')
df['Difference'] = df.Open.diff()
print(df)
The output:
Open High Low Value Volume Difference
Dates
2018-03-01 09:30:00 0.83064 0.83121 0.83064 0.0 0.0 NaN
2018-03-01 09:31:00 0.83121 0.83128 0.83114 0.0 0.0 0.00057
2018-03-01 09:32:00 0.83128 0.83161 0.83126 0.0 0.0 0.00007
2018-03-01 09:33:00 0.83161 0.83169 0.83161 0.0 0.0 0.00033
2018-03-01 09:34:00 0.83169 0.83169 0.83145 0.0 0.0 0.00008
Answer:
df.fillna(method='pad')
by default won’t change your dataframe, you need to redefine it with df = df.fillna(method='pad')
.
Tags: excel, pythonpython