Python – Pandas: Conditionally inserts rows into a DataFrame while traversing them

Pandas: Conditionally inserts rows into a DataFrame while traversing them… here is a solution to the problem.

Pandas: Conditionally inserts rows into a DataFrame while traversing them

When iterating through the rows of a specific column in a Pandas DataFrame, I want to add a new row below the current iteration row if the cells in the current iteration row meet certain criteria.

For example:

df = pd. DataFrame(data = {'A': [0.15, 0.15, 0.7], 'B': [1500, 1500, 7000]})

Data frame:

      A     B
0  0.15  1500
1  0.15  1500
2  0.70  7000

Try:

y = 100                             #An example scalar

i = 1

for x in df['A']:
    if x is not None:               #Values in 'A' are filled atm, but not necessarily.
        df.loc[i] = [None, x*y]     #Should insert None into 'A', and product into 'B'.
        df.index = df.index + 1     #Shift index? According to this S/O answer: https://stackoverflow.com/a/24284680/4909923
    i = i + 1

df.sort_index(inplace=True)         #Sort index?

So far I haven’t succeeded; You get a shift index number that doesn’t start at 0, and the rows don’t seem to be inserted in order:

      A     B
3  0.15  1500
4   NaN    70
5  0.70  7000

I tried various variants, tried using applymap with a lambda function, but couldn’t get it to work.

Desired result:

      A     B
0  0.15  1500
1  None  15
2  0.15  1500
3  None  15
4  0.70  7000
5  None  70

Solution

I believe you can use:

df = pd. DataFrame(data = {'A': [0.15, 0.15, 0.7], 
                          'B': [1500, 1500, 7000],
                          'C': [100, 200, 400]})

v = 100
L = []
for i, x in df.to_dict('index').items():
    print (x)
    #append dictionary
    L.append(x)
    #append new dictionary, for missing keys ('B, C') DataFrame constructor add NaNs 
    L.append({'A':x['A'] * v})

df = pd. DataFrame(L)
print (df)
       A       B      C
0   0.15  1500.0  100.0
1  15.00     NaN    NaN
2   0.15  1500.0  200.0
3  15.00     NaN    NaN
4   0.70  7000.0  400.0
5  70.00     NaN    NaN

Related Problems and Solutions