delete row of a table if it is sunday in python

You can delete a row from a table in Python using the pandas library if it meets a specific condition. To delete rows that correspond to a Sunday in a given column, you can follow these steps:

  1. Import the required libraries:
main.py
import pandas as pd
from datetime import datetime
50 chars
3 lines
  1. Create a pandas DataFrame with your table data:
main.py
data = {'Date': ['2022-09-01', '2022-09-02', '2022-09-03', '2022-09-04', '2022-09-05'],
        'Value': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data)
151 chars
4 lines
  1. Convert the 'Date' column to the pandas DateTime format:
main.py
df['Date'] = pd.to_datetime(df['Date'])
40 chars
2 lines
  1. Filter the DataFrame to keep the rows where the 'Date' column is not a Sunday:
main.py
df = df[df['Date'].dt.day_name() != 'Sunday']
46 chars
2 lines
  1. Print the updated DataFrame to verify that the rows with Sunday dates have been deleted:
main.py
print(df)
10 chars
2 lines

Here's the complete code:

main.py
import pandas as pd
from datetime import datetime

data = {'Date': ['2022-09-01', '2022-09-02', '2022-09-03', '2022-09-04', '2022-09-05'],
        'Value': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data)

df['Date'] = pd.to_datetime(df['Date'])
df = df[df['Date'].dt.day_name() != 'Sunday']

print(df)
300 chars
12 lines

This will remove any rows from the table where the date falls on a Sunday.

related categories

gistlibby LogSnag