Python Forum
Obtaining Correct Date In Pandas DataFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Obtaining Correct Date In Pandas DataFrame
#1
Hi there,

I have a Python Code, which runs fine, but I want the Correct complete Date shown, in the Date Column, in each Row of the DataFrame Output.

Due to the layout of the Schedule Table, at that particular URL, only the day is shown in the Date Column, in each Row displayed.

Here is the Code :-

import pandas as pd
import requests
from bs4 import BeautifulSoup

res = requests.get("http://web.archive.org/web/20041020000138/http://www.raf.mod.uk/bbmf/displaydates.html")
soup = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table', align="CENTER")[0]
df = pd.read_html(str(table))

df = df[0]
df = df.rename(columns=df.iloc[0])
df = df.iloc[2:]
df.head(15)
pd.options.display.max_rows = 1000

display = df[(df['Location'].str.contains('[a-zA-Z]')) & (df['Lancaster'].str.contains('X')) & (df['Spitfire'].str.contains('X', na=True)) & (df['Dakota'].str.contains('X', na=True))]    
display 

#display.drop('Dakota', axis=1, inplace=True)
display.dropna(subset=['Spitfire', 'Hurricane'], how='all')
And this is the Output :-

Output:
Date Location Lancaster Spitfire Hurricane Dakota 21 14 Digby X X X NaN 22 15 Harlaxton X X X NaN 23 15 Coleraine X X X NaN 30 16 New Leake X X X X 31 16 Blackpool X X X NaN 32 16 Elvington X X X NaN 46 30 Conington X X X NaN 47 30 Enfield X X X NaN 48 30 Southend X X X NaN 51 31 Southend X X X NaN 53 31 Sharnbrook X X X NaN 54 31 Peterborough X X X NaN 62 5 Portsmouth X X x 2 NaN NaN 63 5 Sandown (IoW) X X x 2 NaN NaN 64 5 Yarmouth Harbour (IoW) X X x 2 NaN NaN 68 6 Newhaven Fort X X x 2 NaN NaN 71 6 Westerham X X x 2 NaN NaN 72 6 Arromanche X X x 2 NaN NaN 75 7 Normandy X X x 2 NaN X 79 12 Digby X X NaN NaN 80 12 Bracebridge Heath X X NaN NaN 81 12 Newark X X x 2 X X 82 12 Castle Donington X X NaN NaN 87 13 Barrow in Furness X X X NaN 88 13 Cosford X X X X 89 13 Loughborough X X X NaN 91 13 Little Bytham X X NaN NaN 128 26 Woodston X X X NaN 129 26 Barrowden X X X NaN 130 26 Skillington X X X NaN 131 26 East Bridgeford X X X NaN 132 26 Waddington X X X X 139 27 Waddington X X X X 151 3 Tattershall X X NaN NaN 162 3 Branston X X X NaN 163 3 Ripley Castle X X X NaN 175 4 Spilsby X X NaN NaN 185 10 East Fortune X X X NaN 186 10 Thirsk X X X NaN 203 14 Culdrose X X X NaN 204 14 Shrivenham X X X NaN 208 17 Old Brampton X X X NaN 209 17 Glenridding X X X NaN 210 17 Carlisle X X X NaN 219 23 Llandiloes X X X X 220 23 Benson X NaN X NaN 221 24 Farnborough X X X NaN 222 24 North Coates X X X NaN 223 24 Scunthorpe X X X NaN 224 24 Breighton X X X NaN 225 24 Sunderland X X X NaN 226 25 Sunderland X X X NaN 227 25 York X X X NaN 228 25 Breighton X X X NaN 229 25 Luddington X X X NaN 230 25 North Coates X X X NaN 231 25 Sutton on Sea X X X NaN 235 29 Lowestoft X X X NaN 236 30 Lowestoft X X X NaN 240 31 Lincoln Showground X X X NaN 241 31 Kielder Forest X X X NaN 242 31 Windermere X X X NaN 244 1 Windermere X X X NaN 245 1 Cudworth X X X NaN 246 1 Doncaster X X X NaN 247 1 Lincoln Showground X X X NaN 250 13 Eastbourne X X X NaN 251 14 Eastbourne X X X NaN 258 15 Eastbourne X X X NaN 260 15 Barnack X X X NaN 271 21 Shepway X X X NaN 274 22 Falaise X X NaN NaN 277 27 Dartmouth X X X NaN 278 28 Henstridge X X X NaN 279 28 Shoreham X X X NaN 280 28 Kent Showground X X X NaN 281 29 Shoreham X X X NaN 285 29 Little Gransden X X X NaN 289 30 Aylsham X X X NaN 291 30 Grimsthorpe X NaN X NaN 303 4 Duxford X X X NaN 304 4 Goodwood X X X NaN 305 4 Fareham X X X NaN 306 5 Goodwood X X X NaN 308 5 Duxford X X X NaN 315 9 Guernsey X X X X 316 9 Jersey X X X X 319 11 Southport X X X NaN 320 11 Morecambe X X X NaN 321 11 Leuchars X X X NaN 325 12 Morecambe X X X NaN 326 12 Southport X X X NaN 350 18 Yeovilton X X X NaN 351 18 Little Casterton X X X NaN 364 25 Lichfield X X NaN NaN
If you type, and go to the Url, you will see where the Month Is displayed. What should I type, so that all the full Dates are shown, in the Date Column ?

For example for the Venue Ripley the date would show as, 02/05/2004

For Blackpool it would show as 16/05/2004

And that pattern, supplied for every Row, in the Output DataFrame ?

Sorry in the Output I posted, the layout is incorrect. If you run the Code, in Jupyter Notebook, you will see,
what I was trying to post.

Any help would be appreciated

Regards

Eddie Winch
Reply


Messages In This Thread
Obtaining Correct Date In Pandas DataFrame - by eddywinch82 - Jan-06-2020, 06:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add NER output to pandas dataframe dg3000 0 96 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 1,055 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Pandas read csv file in 'date/time' chunks MorganSamage 4 1,709 Feb-13-2023, 11:24 AM
Last Post: MorganSamage
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,127 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,447 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,327 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,271 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,808 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,558 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,314 Aug-14-2021, 12:38 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020