Debug School

rakesh kumar
rakesh kumar

Posted on

List down different way to filtering data in django.

Filter data based on a single condition using Boolean indexing:

import pandas as pd
from myapp.models import MyModel

data = pd.read_csv('data.csv')
filtered_data = data[data['column1'] > 10]  # Filter data where 'column1' is greater than 10
for index, row in filtered_data.iterrows():
    my_model = MyModel(field1=row['column1'], field2=row['column2'])
    my_model.save()
Enter fullscreen mode Exit fullscreen mode

Filter data based on multiple conditions using Boolean indexing:

import pandas as pd
from myapp.models import MyModel

data = pd.read_csv('data.csv')
filtered_data = data[(data['column1'] > 10) & (data['column2'] == 'A')]  # Filter data based on multiple conditions
for index, row in filtered_data.iterrows():
    my_model = MyModel(field1=row['column1'], field2=row['column2'])
    my_model.save()
Enter fullscreen mode Exit fullscreen mode

Filter data using the query() method:

import pandas as pd
from myapp.models import MyModel

data = pd.read_csv('data.csv')
filtered_data = data.query("column1 > 10")  # Filter data using a query
for index, row in filtered_data.iterrows():
    my_model = MyModel(field1=row['column1'], field2=row['column2'])
    my_model.save()
Enter fullscreen mode Exit fullscreen mode

Filter data using the isin() method:

import pandas as pd
from myapp.models import MyModel

data = pd.read_csv('data.csv')
filtered_data = data[data['column1'].isin(['A', 'B', 'C'])]  # Filter data based on specific values in 'column1'
for index, row in filtered_data.iterrows():
    my_model = MyModel(field1=row['column1'], field2=row['column2'])
    my_model.save()
Enter fullscreen mode Exit fullscreen mode

Filter data using string methods:

import pandas as pd
from myapp.models import MyModel

data = pd.read_csv('data.csv')
filtered_data = data[data['column1'].str.contains('keyword')]  # Filter data based on a keyword in 'column1'
for index, row in filtered_data.iterrows():
    my_model = MyModel(field1=row['column1'], field2=row['column2'])
    my_model.save()
Enter fullscreen mode Exit fullscreen mode

Filter data using regular expressions:

import pandas as pd
import re
from myapp.models import MyModel

data = pd.read_csv('data.csv')
filtered_data = data[data['column1'].str.contains(r'regex_pattern', flags=re.IGNORECASE, regex=True)]  # Filter data using a regex pattern in 'column1'
for index, row in filtered_data.iterrows():
    my_model = MyModel(field1=row['column1'], field2=row['column2'])
    my_model.save()
Enter fullscreen mode Exit fullscreen mode

Filter data based on date or time:

import pandas as pd
from myapp.models import MyModel

data = pd.read_csv('data.csv')
data['date_column'] = pd.to_datetime(data['date_column'])  # Convert 'date_column' to datetime if necessary
filtered_data = data[data['date_column'] > '2022-01-01']  # Filter data based on a date condition
for index, row in filtered_data.iterrows():
    my_model = MyModel(field1=row['column1'], field2=row['column2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)