Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am trying to add rows to the existing Data frame and keep the existing one.

The data looks like this:

product price   max_move_%

  1     100      10

I run loops this way:

for i, row in df_merged.iterrows():

for a in range((row['max_move_%']) * (- 1), row['max_move_%']):

    df_merged['price_new'] = df_merged['price'] * (1 - a / 100.00)

My desired output is:

product price   max_move_%  true_move     price_new

1       100      10          -10            90

1       100      10          -9             91

 .....

1       100      10          10             110

There is no change in the data frame everything looks the same. Can anyone suggest to me how do I add new values to the columns and how should I solve it?

This is the code I tried:

df_loop = []

for i, row in df_merged.iterrows():

    for a in range((row['max_move_%']) * (- 1), row['max_move_%'] + 1):

    df_loop.append((df_merged['product'], df_merged['price'], f_merged['max_move_%'],a))

pd.DataFrame(df_loop, columns=('product','price','max_move_%','price_new'))

But it doesn't work as I wanted.

1 Answer

0 votes
by (36.8k points)
edited by

I created a data frame with 5 columns as you desired, so added rows into this one as shown in the below code:

import pandas as pd

df_merged = pd.DataFrame(data=[[1, 100, 10]], columns=['product', 'price', 'max_move_%'])

print(df_merged)

#    product  price  max_move_%

# 0        1    100          10

new_columns = ['product', 'price', 'max_move_%', 'true_move', 'price_new']

df_new = pd.DataFrame(columns=new_columns)

idx = 0

for i, row in df_merged.iterrows():

    for true_move in range((row['max_move_%']) * (- 1), row['max_move_%']+1):

        price_new = df_merged.iloc[i]['price'] * (1 + true_move / 100.00)

        df_new.loc[idx] = row.values.tolist() + [true_move, price_new]

        idx += 1

print(df_new)

#     product  price  max_move_%  true_move  price_new

# 0       1.0  100.0        10.0      -10.0       90.0

# 1       1.0  100.0        10.0       -9.0       91.0

# 2       1.0  100.0        10.0       -8.0       92.0

# 3       1.0  100.0        10.0       -7.0       93.0

# 4       1.0  100.0        10.0       -6.0       94.0

# 5       1.0  100.0        10.0       -5.0       95.0

# 6       1.0  100.0        10.0       -4.0       96.0

# 7       1.0  100.0        10.0       -3.0       97.0

# 8       1.0  100.0        10.0       -2.0       98.0

# 9       1.0  100.0        10.0       -1.0       99.0

# 10      1.0  100.0        10.0        0.0      100.0

# 11      1.0  100.0        10.0        1.0      101.0

# 12      1.0  100.0        10.0        2.0      102.0

# 13      1.0  100.0        10.0        3.0      103.0

# 14      1.0  100.0        10.0        4.0      104.0

# 15      1.0  100.0        10.0        5.0      105.0

# 16      1.0  100.0        10.0        6.0      106.0

# 17      1.0  100.0        10.0        7.0      107.0

# 18      1.0  100.0        10.0        8.0      108.0

# 19      1.0  100.0        10.0        9.0      109.0

# 20      1.0  100.0        10.0       10.0      110.0

I modified your % to an equation for evaluating price_new column values.

Learn Python for Data Science Course to improve your technical knowledge.

If you wish to learn more about Python, visit the Python tutorial and Python course by Intellipaat.

Browse Categories

...