I have a pandas dataframe which I have collected from a MongoDB.
The column names are a series of dates, ie. 4/7/20, 4/8/20, etc.
What I want to do is find the most recent date which has a column name the same as the date, because I want to delete all other date columns before writing it to a PostgreSQL database.
I was intending:
- Set a variable with today's date
- Loop through the column names comparing with today's date
- If exists, retain the variable name
- If it does not, reduce the date by 1 and check again until I get a match.
I am trying to get a list of column names from the dataframe, but when I run
collection = client['DB_Name']['DB_Collection']
df = collection.find()
data_pandas = pd.DataFrame(list(df))
index_list = list(data_pandas.index.values.tolist())
today = date.today()
today = today.strftime('X%m/X%d/%Y').replace('X0','X').replace('X','')
print(df.columns)
I get an error:
'Cursor' object has no attribute 'columns'
The data frame looks fine from the IDE. What can I do to resolve this?