How to access a dataframe from a Python dataframe list through a cell from a date column in the dataframe

40 Views Asked by At

I have created a list (df) which contains some dataframes after importing csv files. Instead of accessing this dataframes using df[0], df[1] etc, I would like to access them in a much easier way with something like df[20/04/22] or df[date=='20/04/22] or something similar. I am really new to Python and programming, thank you very much in advance. I attach the simplified code (contains only 2 items in the list) for simplyfying reasons.

I came up with two ways of achieving that but each time I have some trouble realising them.

  1. Through my directory path names. Each csv (dataframe) file name includes the date in each original name file, something like : "5f05d5d83a442d4f78db0a19_2022-04-01.csv"
  2. Each csv (dataframe), includes a date column (object type) which I have changed to datetime64 type so I can work with plots. So, I thought that maybe through this column what I ask would be possible.
import pandas as pd
import numpy as np
import matplotlib 
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
from datetime import date
from datetime import time
from pandas.tseries.offsets import DateOffset
import glob
import os

path = "C:/Users/dsdadsdsaa/"
all_files = glob.glob(path + '*.csv')
df = []
for filename in all_files:
    dataframe = pd.read_csv(filename, index_col=None, header=0)
    df.append(dataframe)

for i in range(0,2):
    df[i]['date'] = pd.to_datetime(df[i]['date'])
    df[i]['time'] = pd.to_datetime(df[i]['time'])

df[0]
0

There are 0 best solutions below