Python Forum
Data extraction from a table based on column and row names - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Data extraction from a table based on column and row names (/thread-31916.html)



Data extraction from a table based on column and row names - tgottsc1 - Jan-09-2021

Hello,

I have a basic question regarding the extraction of data from a table. Google did not bring up the desired result, so I would be happy if someone could help me further.

I use the following code to extract stock price data of Ford Motor Company from a website:

import pandas as pd
 
url = 'https://www.ariva.de/ford_motor-aktie/kurs'
df = pd.read_html(url)
df = df[3]
df
Output:
Handelsplatz Letzter Unnamed: 2 Änderung Änderung.1 Vortag letzte Stk. Tag-Stk. Kursspanne Zeit Unnamed: 10 Unnamed: 11 0 Tradegate 7,34 € NaN -0,57% NaN 7,382 € 400 15.279 7,27 - 7,489 08.01.21 NaN Hist. Kurse 1 Gettex 7,343 € NaN -0,20% NaN 7,358 € 0 166 7,308 - 7,459 08.01.21 NaN Hist. Kurse 2 L&S RT 7,362 € NaN 0 % NaN 7,362 € 0 0 7,362 - 7,362 12:35:15 NaN Hist. Kurse 3 HypoVereinsbank 8,995 $ NaN -0,66% NaN 9,055 $ 0 0 8,89 - 9,14 08.01.21 NaN Hist. Kurse 4 Quotrix 7,443 € NaN +1,50% NaN 7,333 € 0 0 7,443 - 7,443 08.01.21 NaN Hist. Kurse 5 NYSE 9,00 $ NaN -0,66% NaN 9,06 $ 1.954.291 12.467.481 8,89 - 9,14 08.01.21 NaN Hist. Kurse
Now I would like to know the NYSE price (i.e. row element: 'NYSE', column element: 'Letzter'). However, the order of the rows can sometimes change, so I can't directly say 'I need the price ('Letzter') from row 5'. Instead, I'd like to set it up so that it extracts the NYSE price regardless of which row has NYSE in it (NYSE will only appear once).

I would be grateful for any help!

Many thanks in advance.

Tim


RE: Data extraction from a table based on column and row names - buran - Jan-09-2021

print(df['Letzter'][df['Handelsplatz']=='NYSE'])
# or
print(df.query('Handelsplatz=="NYSE"')['Letzter'])