I have a pandas dataframe similar to the one below:
| Date | Value1 | Value2
0 | 2021-01 | 10.90 | 2.34
1 | 2021-02 | 12.85 | 4.65
2 | 2021-03 | 15.56 | 6.11
3 | 2021-04 | 18.23 | 8.62
Sample Code to create dataframe:
import pandas as pd
df = pd.DataFrame({
"Date": ['2021-01', '2021-02', '2021-03', '2021-04'],
"Value1": [10.90, 12.85, 15.56, 18.23],
"Value2": [2.34, 4.65, 6.11, 8.62]
})
I want to extract the value from the Value1 and Value2 columns using a specific Date without making Date as index of the dataframe.
What I have done, works fine but looking for some better approach.
Single line code will be much appreciated for this task.
My approach:
value1_2021_01 = df[df["Date"] == '2021-01'][['Value1']].iat[0,0]
value2_2021_04 = df[df["Date"] == '2021-04'][['Value2']].iat[0,0]
Please note that the Date columns will always have unique values.
df.loc[df["Date"].isin(['2021-01', '2021-04']), ['Value1', 'Value2']]?pandas.DataFrame.. I need thefloatvalue. Adding.iat[0,0]at the end does the job but it more or less comes down to the same solution as mine.