I am very new to coding and I'm trying to view data from FastF1. I am attempting to do this using Python and Jupyter Lab. Whenever I print two columns using the code below:
import fastf1
from fastf1 import plotting
import pandas as pd
plotting.setup_mpl()
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
fp3 = fastf1.get_session(2022,'Monza','FP3')fp3.load()
fp3_laps = fp3.laps
fp3_d1 = fp3_laps.loc[fp3_laps['Driver']=='VER']
print([[fp3_d1.LapTime, fp3_d1.LapNumber]])
My aim would be to get have both the LapTime
and LapNumber
columns next to each other.
CodePudding user response:
fp3 = fastf1.get_session(2022,'Monza','FP3')fp3.load()
fp3_laps = fp3.laps
fp3_d1 = fp3_laps.loc[fp3_laps['Driver']=='VER']
print([[fp3_d1.LapTime, fp3_d1.LapNumber]])
From the above code, it looks like fp3_d1
is a dataframe. So fp3_d1.LapTime
is a pandas series, in other words, selecting a column from the dataframe. It's the same as fp3_d1['LapTime']
(I prefer this notation). You can check with type(fp3_d1['LapTime'])
to get "pandas.core.series.Series".
And fp3_d1.LapNumber
(the same as fp3_d1['LapNumber']
) is another pandas series. The last line of the code is simply to stack 2 pandas series vertically, becoming "a list of 1 list of 2 items", notice the double square brackets [[
in front and behind ]]
and a comma ,
in between.
The syntax df[['col1', 'col2']]
is to extract 2 columns from the dataframe, forming a subset of the full dataframe with only the columns col1
and col2
side by side. You can check with df[['col1', 'col2']]
to get "pandas.core.frame.DataFrame".
Your aim to get both LapTime
and LapNumber
columns next to each other, is to get a subset of the dataframe with these columns. So the code should be:
print(fp3_d1[['LapTime', 'LapNumber']])
CodePudding user response:
You can do dataframe subsetting like fp3_d1[['LapTime', 'LapNumber']]
. This will output a subset of the dataframe with the 2 columns indicated.
Refer to pandas documentation for more details