Reputation: 193
I have a pd df and I want to select one of the columns by matching the column names.
For example, if I have a defined value called "division =2". I want to use that to select related column from the following table. In this case, I want to get column df.iloc[:, 2].
How do I do this?
division=2
Year 1 2 3 4
0 2024 1.007351 1.098082 1.033620 0.938746
1 2025 1.023808 1.117399 1.036366 0.936205
2 2026 1.036785 1.133247 1.040184 0.934735
Upvotes: 0
Views: 364
Reputation: 9701
You are very nearly there! See below. You can use your division
variable in place of the column name (or locator, in this case).
A word of caution:
It's important to note, the difference in column name and locator. The OP asks for column name but is using the locator (df.iloc
) in the question.
Locator:
.iloc[:, 2]
will return all rows in the third column (numbering starts at 0). And this can vary as this DataFrame was created via a dict
, which does not retain order.
Column Name:
df[2]
or df.loc[:, 2]
will return all rows in the column named 2.
import pandas as pd
division = 2
data = {'Year': [2024, 2025, 2026],
1: [1.007351, 1.023808, 1.036785],
2: [1.098082, 1.117399, 1.133247],
3: [1.033620, 1.036366, 1.040184],
4: [0.938746, 0.936206, 0.934735]}
df = pd.DataFrame(data)
# Display DataFrame
print(df)
# 1) Display the output of column named 2.
print(df[division])
# 2) Display the output of column 2.
print(df.iloc[:, division])
DataFrame contents:
1 2 3 4 Year
0 1.007351 1.098082 1.033620 0.938746 2024
1 1.023808 1.117399 1.036366 0.936206 2025
2 1.036785 1.133247 1.040184 0.934735 2026
1) Output for column name:
print(df[division])
0 1.098082
1 1.117399
2 1.133247
Name: 2, dtype: float64
2) Output for column locator:
print(df.iloc[:, division])
0 1.033620
1 1.036366
2 1.040184
Name: 3, dtype: float64
Upvotes: 2