If empty take from previous row from list of dictionaries

Viewed 53

I have this list of dictionaries:

[{
'time': '2022-01-01', 'pl': 0.0, 'accountBalance': 500.0, 'orderID': 500.0}, {
'time': '2022-01-25', 'pl': 6.3505, 'accountBalance': nan, 'orderID': 506.35}, {
'time': '2022-01-26', 'pl': 23.2703, 'accountBalance': 505.9298, 'orderID': 501.03}, {
'time': '2022-01-27', 'pl': 16.68, 'accountBalance': 521.4393, 'orderID': 502.07}, {
'time': '2022-01-28', 'pl': -2.29, 'accountBalance': 519.1493, 'orderID': nan}, {
'time': '2022-01-31', 'pl': -0.89, 'accountBalance': 518.2593, 'orderID': nan
}]

I want to replace nan by the value from the previous row like this :

[{
'time': '2022-01-01', 'pl': 0.0, 'accountBalance': 500.0, 'orderID': 500.0}, {
'time': '2022-01-25', 'pl': 6.3505, 'accountBalance': 500.0, 'orderID': 506.35}, {
'time': '2022-01-26', 'pl': 23.2703, 'accountBalance': 505.9298, 'orderID': 501.03}, {
'time': '2022-01-27', 'pl': 16.68, 'accountBalance': 521.4393, 'orderID': 502.07}, {
'time': '2022-01-28', 'pl': -2.29, 'accountBalance': 519.1493, 'orderID': 502.07}, {
'time': '2022-01-31', 'pl': -0.89, 'accountBalance': 518.2593, 'orderID': 502.07
}]

I tried with Pandas and "df.fillna(method='ffill')" but I have an error saying 'list' object has no attribute 'fillna'. I think it's because it's not a csv.

2 Answers

You need to convert your list into a DataFrame first.

import pandas as pd

data = [{
'time': '2022-01-01', 'pl': 0.0, 'accountBalance': 500.0, 'orderID': 500.0}, {
'time': '2022-01-25', 'pl': 6.3505, 'accountBalance': None, 'orderID': 506.35}, {
'time': '2022-01-26', 'pl': 23.2703, 'accountBalance': 505.9298, 'orderID': 501.03}, {
'time': '2022-01-27', 'pl': 16.68, 'accountBalance': 521.4393, 'orderID': 502.07}, {
'time': '2022-01-28', 'pl': -2.29, 'accountBalance': 519.1493, 'orderID': None}, {
'time': '2022-01-31', 'pl': -0.89, 'accountBalance': 518.2593, 'orderID': None
}]

df = pd.DataFrame(data)

df.fillna(method="ffill")

Try this:

l = <your_list>

for i, d in enumerate(l[1:], 1):
    for key, val in d.items():
        if val == nan:
            d[key] = l[i - 1][key]
Related