Home > Enterprise >  Python || Not able to convert my for loop o/p into a Dataframe
Python || Not able to convert my for loop o/p into a Dataframe

Time:08-03

If I am running a for loop and if else loop together on a column of data frame. I am getting the output .. but they are just list of numbers . how can I create that list into a data frame and add it into the original data frame so that I can compute other operations on that column ?

CodePudding user response:

Preparation:

import pandas as pd
import numpy as np
rng = np.random.default_rng(42)
df = pd.DataFrame({
    'discounted_price': rng.integers(10,100, 10)*10
})
df
###
   discounted_price
0               180
1               790
2               680
3               490
4               480
5               870
6               170
7               720
8               280
9               180



Conditional manipulation based on values:

condlist = [df['discounted_price'] > 600, df['discounted_price'] > 350, df['discounted_price'] > 100, df['discounted_price'] <= 100]
choicelist = [df['discounted_price'] * 0.25, df['discounted_price'] * 0.15, df['discounted_price'] * 0.1, df['discounted_price'] * 0.05]
df['output'] = np.select(condlist, choicelist)
df
###
   discounted_price  output
0               180    18.0
1               790   197.5
2               680   170.0
3               490    73.5
4               480    72.0
5               870   217.5
6               170    17.0
7               720   180.0
8               280    28.0
9               180    18.0

CodePudding user response:

Lets create a dataframe out of a dictionary as example:

# Dictionary with list object in values
details = { 'Name' : ['Juan', 'Domingo', 'Eva', 'Maria'],
            'Age' : [33, 21, 22, 19],
            'University' : ['UNLP', 'UBA', 'UNC', 'UNR'] }
  
# creating a Dataframe object 
df = pd.DataFrame(details)

Solution #1

Now lets do your loop:

filtered_age = []
for age in df["Age"]:
    if age <= 22:
        filtered_age.append(age)

You can then use your new list ("filtered_age") to create a new dictonary and finally a new dataframe:

filtered_details = {'Filtered age' : filtered_age}
df_age = pd.DataFrame(new_df)
Filtered age
0 21
1 22
2 19

Solution #2

You can also use apply to create a True/False filter like this:

age_filter = df['Age'].apply(lambda x: True if x < 22 else False)
df[age_filter]
Name Age University
1 Domingo 21 UBA
2 Eva 22 UNC
3 Maria 19 UNR
  • Related