close

How to create a new data frame based on conditions from another data frame

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to create a new data frame based on conditions from another data frame in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to create a new data frame based on conditions from another data frame?

  1. How to create a new data frame based on conditions from another data frame?

    In the current version of Pandas, the .ix has deprecated; instead, use .loc.
    temp_df = df_complete.loc[]

  2. create a new data frame based on conditions from another data frame

    In the current version of Pandas, the .ix has deprecated; instead, use .loc.
    temp_df = df_complete.loc[]

Method 1

I think you need boolean indexing with loc for selecting only columns col a and col c:

temp_df = df_complete.loc[(df_complete['type'] == 'NDD') & 
                         (df_complete['writer'] == 'Mary') & 
                         (df_complete['status'] != '7'), ['col a','col c']]
#rename columns
temp_df = temp_df.rename(columns={'col a':'col A','col c':'col C'})
#add new column 
temp_df['col B'] = 'good'
#reorder columns
temp_df = temp_df[['col A','col B','col C']]

Sample:

df_complete = pd.DataFrame({'type':  ['NDD','NDD','NT'],
                            'writer':['Mary','Mary','John'],
                            'status':['4','5','6'],
                            'col a': [1,3,5],
                            'col b': [5,3,6],
                            'col c': [7,4,3]}, index=[3,4,5])

print (df_complete)
   col a  col b  col c status type writer
3      1      5      7      4  NDD   Mary
4      3      3      4      5  NDD   Mary
5      5      6      3      6   NT   John

temp_df = df_complete.loc[(df_complete['type'] == 'NDD') & 
                         (df_complete['writer'] == 'Mary') & 
                         (df_complete['status'] != '7'), ['col a','col c']]

print (temp_df)  
   col a  col c
3      1      7
4      3      4

temp_df = temp_df.rename(columns={'col a':'col A','col c':'col C'})
#add new column 
temp_df['col B'] = 'good'
#reorder columns
temp_df = temp_df[['col A','col B','col C']]
print (temp_df)  
   col A col B  col C
3      1  good      7
4      3  good      4

Method 2

In the current version of Pandas, the .ix has deprecated; instead, use .loc.

temp_df = df_complete.loc[]

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read