Home > database >  inconsistent index with different periods get just one type
inconsistent index with different periods get just one type

Time:10-14

I download a df from the internet which is basically like this. I just want to keep the rows which are daily values. Is there any possibility of doing this, so every time I download the df I get until the last daily value?

Date name
21-02-2013 1231
22-02-2013 323
23-02-2013 343
... ...
12-10-2020 432
Monthly
02-2013 2655
03-2013 321
... ...
Yearly
Total 2013 3213
Total 2020 543

CodePudding user response:

you could perform a regex match on the 'Date' column; for given example data

df
Out[10]: 
          Date   name
0   21-02-2013   1231
1   22-02-2013    323
2   23-02-2013    343
3          ...    ...
4   12-10-2020    432
5      Monthly    NaN
6      02-2013   2655
7      03-2013    321
8          ...    ...
9       Yearly    NaN
10  Total 2013   3213
11  Total 2020    543

that would be

df.loc[df['Date'].str.match('[0-9]{2}\-[0-9]{2}\-[0-9]{4}')]
Out[11]: 
         Date   name
0  21-02-2013   1231
1  22-02-2013    323
2  23-02-2013    343
4  12-10-2020    432

# or if you need it to be even more specific:
# df.loc[df['Date'].str.fullmatch('[0-9]{2}\-[0-9]{2}\-[0-9]{4}')]
  • Related