0

I have a dataframe with 6 column the first one of which represents a Date.

I want to select only the rows with a specific date represented in the first column, how should I do ?

The dataframe is loaded via read_csv and contains 6 columns , as strings , the first one represents a Date and the remaining ones are representing integers

I was using :

df['Data' == '....'] which I found somewhere around the net

Initially the first column , 'Data', was included in the index_col=[] list in the read_csv : I kept getting error on that same column, so figuring it could be caused by the index_col list, I completely eliminated it, to no avail. After that I tried thousands of other methods , none of them worked .

Is there a way to do it or should i simply give up the idea ?

This is the code :

l = [{'Data' : '2023-07-11','Val1':'2','Val2' :'23', 'Val3':'2','P':'0','Totals':'12'},
     {'Data' : '2023-08-01','Val1':'3','Val2' :'0', 'Val3':'21','P':'0','Totals':'2'},
     {'Data' : '2023-01-09','Val1':'4','Val2' :'41', 'Val3':'51','P':'1','Totals':''},
     {'Data' : '2023-04-12','Val1':'5','Val2' :'210', 'Val3':'30','P':'0','Totals':'1'},
     ]

orig_df_stats = pd.DataFrame(l)


print (orig_df_stats)

print (orig_df_stats['Data' == '2023-07-11'])
Booji Boy
  • 15
  • 4
  • dput () doesn't seem to be a legal call in Python.... – Booji Boy Jul 30 '23 at 15:32
  • Please [edit] your post with a [reproducible example](https://stackoverflow.com/q/20109391/1422451). Better to show than tell us your sample data and code block in the body of your post. – Parfait Jul 30 '23 at 16:31
  • Oops sorry I overlooked the part where it said python try df.to_dict() instead – Mark Jul 30 '23 at 22:57
  • You mean using df.to_dict() to visualize the dataframe ? or to implement the select operation that I don't seem to be able to ? – Booji Boy Jul 31 '23 at 04:40
  • so you can add reproducible data to your question – Mark Jul 31 '23 at 06:43

1 Answers1

0

What you're looking for is this:

orig_df_stats[orig_df_stats['Data'] == '2023-07-11']
Mark
  • 7,785
  • 2
  • 14
  • 34
  • why doesn't the same call work when the dataframe is loaded from a .csv file ? – Booji Boy Jul 31 '23 at 12:47
  • uhh include the csv file data? @BoojiBoy – Mark Jul 31 '23 at 14:51
  • Here are a couple of rows from the file : Data Val1 Val2 Val3 P Totals 2023-07-15 00:00:00.000 3 45 0 300 300 2023-07-15 00:00:00.000 30 36 0 300 300 the file seems to be correctly formatted as I am able to load it as a dataframe and visualize it with a print() statement – Booji Boy Jul 31 '23 at 15:21