Get column index from column name in python pandas
Sure, you can use .get_loc()
:
In [45]: df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})
In [46]: df.columns
Out[46]: Index([apple, orange, pear], dtype=object)
In [47]: df.columns.get_loc("pear")
Out[47]: 2
although to be honest I don't often need this myself. Usually access by name does what I want it to (df["pear"]
, df[["apple", "orange"]]
, or maybe df.columns.isin(["orange", "pear"])
), although I can definitely see cases where you'd want the index number.
Retrieve name of column from its Index in Pandas
I think you need index columns names by position (python counts from 0
, so for fourth column need 3
):
colname = df.columns[pos]
Sample:
df = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9],
'D':[1,3,5],
'E':[5,3,6],
'F':[7,4,3]})
print (df)
A B C D E F
0 1 4 7 1 5 7
1 2 5 8 3 3 4
2 3 6 9 5 6 3
pos = 3
colname = df.columns[pos]
print (colname)
D
pos = [3,5]
colname = df.columns[pos]
print (colname)
Index(['D', 'F'], dtype='object')
Pandas index column title or name
You can just get/set the index via its name
property
In [7]: df.index.name
Out[7]: 'Index Title'
In [8]: df.index.name = 'foo'
In [9]: df.index.name
Out[9]: 'foo'
In [10]: df
Out[10]:
Column 1
foo
Apples 1
Oranges 2
Puppies 3
Ducks 4
Pandas: Get cell value by row index and column name
Use .loc
to get rows by label and .iloc
to get rows by position:
>>> df.loc[3, 'age']
23
>>> df.iloc[2, df.columns.get_loc('age')]
23
More about Indexing and selecting data
Get column names with corresponding index in python pandas
Simpliest is add pd.Series
constructor:
pd.Series(list(df.columns))
Or convert columns to Series and create default index:
df.columns.to_series().reset_index(drop=True)
Or:
df.columns.to_series(index=False)
Pandas return index and column name based on condition
One option is to reshape the data and filter with the booleans:
df.lt(.25).stack().loc[lambda df: df].index.to_list()
[('A', 'E'), ('C', 'D')]
Get the index name AND column name for each cell in pandas dataframe
you could try melting (pd.melt) your dataframe while keeping the index as your variable :
df = pd.DataFrame(
np.random.choice([True, False], (5, 5)), columns=list("abcde"), index=list("fghij")
)
# a b c d e
# f False True True True False
# g True True False False True
# h False True True False False
# i True True False False False
# j True True False True True
df.reset_index().melt(id_vars='index').query('value == True')
outputs :
index variable value
1 g a True
3 i a True
4 j a True
5 f b True
6 g b True
7 h b True
8 i b True
9 j b True
10 f c True
12 h c True
15 f d True
19 j d True
21 g e True
24 j e True
Trying to get the column indexes with the column names in pandas
i = 0
for col in df.columns:
print(i, col)
i += 1
Efficient way to get indices of column-labels from pd.DataFrame
short_way = [df.columns.get_loc(col) for col in query_cols]
print(sorted(short_way))
# outputs [0, 2, 5, 6]
Related Topics
How to Interact with the Recaptcha Audio Element Using Selenium and Python
Multiprocessing - Pipe VS Queue
Differencebetween JSON.Load() and JSON.Loads() Functions
How to Remove Specific Tag/Sticker/Object from Images Using Opencv
How to Convert Escaped Characters
Call Class Method from Another Class
How Does Python's "Super" Do the Right Thing
Error When Loading Cookies into a Python Request Session
How to Set Layer-Wise Learning Rate in Tensorflow
How to Do Row-To-Column Transposition of Data in CSV Table
Python Regular Expression Pattern * Is Not Working as Expected
How to Find Out My Pythonpath Using Python
How to Write Strategy Pattern in Python Differently Than Example in Wikipedia
Pythonic Way to Combine For-Loop and If-Statement
In Tensorflow, Differencebetween Session.Run() and Tensor.Eval()