How to create independent dataframes in Python

Asked

Viewed 45 times

0

I have a problem in python, as I create independent dataframe? What can I think of:

>>> PREMISSAS['QTD_ESTOQUE_MEDIO']
0          628.5
1        20202.5
2        42902.0
3        50036.0
4         8561.5
          ...   
12646    14356.5
12647     1236.0
12648     9997.5
12649     5181.5
12650     5061.5

>>> PREMISSAS_STK_MAIS_5 = PREMISSAS
>>> PREMISSAS_STK_MAIS_10 = PREMISSAS
>>> PREMISSAS_STK_MENOS_5 = PREMISSAS
>>> PREMISSAS_STK_MENOS_10 = PREMISSAS
>>> 
>>> PREMISSAS_STK_MAIS_5['QTD_ESTOQUE_MEDIO'] = PREMISSAS_STK_MAIS_5['QTD_ESTOQUE_MEDIO']*1.05
>>> PREMISSAS_STK_MAIS_10['QTD_ESTOQUE_MEDIO'] = PREMISSAS_STK_MAIS_10['QTD_ESTOQUE_MEDIO']*1.1
>>> PREMISSAS_STK_MENOS_5['QTD_ESTOQUE_MEDIO'] = PREMISSAS_STK_MENOS_5['QTD_ESTOQUE_MEDIO']*0.95
>>> PREMISSAS_STK_MENOS_10['QTD_ESTOQUE_MEDIO'] = PREMISSAS_STK_MENOS_10['QTD_ESTOQUE_MEDIO']*0.9

>>> PREMISSAS['QTD_ESTOQUE_MEDIO']
0          620.659463
1        19950.473813
2        42366.797550
3        49411.800900
4         8454.695288
             ...     
12646    14177.402663
12647     1220.580900
12648     9872.781188
12649     5116.860788
12650     4998.357788
Name: QTD_ESTOQUE_MEDIO, Length: 12651, dtype: float64
>>> PREMISSAS_STK_MAIS_5['QTD_ESTOQUE_MEDIO']
0          620.659463
1        19950.473813
2        42366.797550
3        49411.800900
4         8454.695288
             ...     
12646    14177.402663
12647     1220.580900
12648     9872.781188
12649     5116.860788
12650     4998.357788
Name: QTD_ESTOQUE_MEDIO, Length: 12651, dtype: float64
>>> PREMISSAS_STK_MENOS_10['QTD_ESTOQUE_MEDIO']
0          620.659463
1        19950.473813
2        42366.797550
3        49411.800900
4         8454.695288
             ...     
12646    14177.402663
12647     1220.580900
12648     9872.781188
12649     5116.860788
12650     4998.357788
Name: QTD_ESTOQUE_MEDIO, Length: 12651, dtype: float64

My goal is to create data frames with different scenarios.

2 answers

0


novo_df = df_original.copy(deep=True)

Variable assignment in Python does not always create a new object, just allocate a new "tag" to an already existing object

  • 1

    Dear, to better understand about changeable and immutable objects. Read the answer of this post

-1

Use conditions to assign a Dataframe to the new

>>> import pandas as pd

>>> df = pd.DataFrame({"PREMISSAS": range(1,100)})

>>> df
    PREMISSAS
0           1
1           2
2           3
3           4
4           5
..        ...
94         95
95         96
96         97
97         98
98         99

Premises greater than 5 and less than 10

>>> df1 = df[(df["PREMISSAS"] < 10) & (df["PREMISSAS"] > 5)]

>>> df1
   PREMISSAS
5          6
6          7
7          8
8          9

I hope it helps

  • I understand that the purpose of the question was to create Dataframes from certain conditions. The person who voted negative on this question could comment on why the answer does not answer? So I can improve the answer.

  • Because the question was not about how to filter a Dataframe, but how to eliminate Dataframe feedback. Dataframe PREMISSAS_STK_MAIS_5 was created from PREMISSAS, but when I changed PREMISSAS_STK_MAIS_5 has occurred from the changes to PREMISSAS.

  • Well, maybe I was betrayed by the phrase at the end of the post Meu objetivo é criar data frames com cenários diferentes.. The answer given by me takes better care than using the method .copy(deep=True), because if you have a Dataframe with millions of lines and make copies of it first and then filter, you can have a "Leak" of memory. The ideal is to create a new Dataframe, as I suggested, already filtering. If you still think the answer does not meet, no problems... Be sure to read the answer on mutable and immutable objects I indicated in your reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.