tedioustortoise
tedioustortoise

Reputation: 269

Iterating in a Pandas dataframe returning a blank result, unsure why?

Instead of performing the calculation and returning a result, the following leaves the Balance column empty. Can anyone understand why?

df = pd.read_sql_query("SELECT * FROM "+n+" ORDER BY Date", conn)
df = df.replace('None', '')
df["DR"] = pd.to_numeric(df["DR"])
df["CR"] = pd.to_numeric(df["CR"])
df["Balance"] = ''
i = 0
df.iloc[i,6] = df.iloc[i,3]-df.iloc[i,4]
for i in range(1,df.shape[0]):
    df.iloc[i,6] = df.iloc[i-1,6]+df.iloc[i,3]-df.iloc[i,4]
df = df.fillna('')
html = df.to_html(header=True,classes="table table-sm table-hover",justify="center",index=False)

python

Upvotes: 0

Views: 80

Answers (1)

vbhargav875
vbhargav875

Reputation: 887

This is because df.iloc[0,4] is NaN (if you see your table it is empty) and hence df.iloc[0,6] will also be NaN.

Since the following rows of column-6(Balance) depend on the previous row, all the values end up being NaN.

In the end you are performing df = df.fillna('') , replacing NaN's with blank space, hence the blank space throughout the column.

Upvotes: 2

Related Questions