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.
Get column index from label in a data frame
you can get the index via grep
and colnames
:
grep("B", colnames(df))
[1] 2
or use
grep("^B$", colnames(df))
[1] 2
to only get the columns called "B" without those who contain a B e.g. "ABC".
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')
Get column index label based on values
I will save the result in a list
because otherwise there could be more than one column with values equal to 1. You can use DataFrame.loc
if all column values must be 1 then you can use:
df.loc[:,df.eq(1).all()].columns.tolist()
Output:
['C3']
if this isn't necessary then use:
df.loc[:,df.eq(1).any()].columns.tolist()
or as suggested @piRSquared, you can select directly from df.columns
:
[*df.columns[df.eq(1).all()]]
get column number from label in DataFrame
You need Index.get_loc
or Index.searchsorted
:
a = df.columns.get_loc('key')
print (a)
1
a = df.columns.searchsorted('key')
print (a)
1
Then iloc
works nice:
a = df.iloc[2, df.columns.get_loc('key')]
print (a)
a
Pandas: Get column index/label of a specific single value in a row from among multiple columns of mutually exclusive choice
You can check for the column values equal 1 across the row and get the column index corresponding to True
(for entries of 1
) by .idxmax()
(with axis=1
for getting column index), as follows:
df['age_group'] = df.eq(1).idxmax(axis=1)
Result:
print(df)
21_30 31_40 40_49 50_59 age_group
0 0 0 0 1 50_59
1 0 1 0 0 31_40
2 0 1 0 0 31_40
3 1 0 0 0 21_30
4 0 0 1 0 40_49
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
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)
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]
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
Related Topics
Randomly Insert Nas into Dataframe Proportionaly
Add Percentage Labels to a Stacked Barplot
How to Stack Error Bars in a Stacked Bar Plot Using Geom_Errorbar
How to Add a Index by Set of Data When Using Rbindlist
Using Parallel's Parlapply: Unable to Access Variables Within Parallel Code
How to Disable "Save Workspace Image" Prompt in R
Grid of Multiple Ggplot2 Plots Which Have Been Made in a for Loop
Ggplot2 - Adding Secondary Y-Axis on Top of a Plot
Exactly Storing Large Integers
Take Sum of a Variable If Combination of Values in Two Other Columns Are Unique
Convert Data Frame with Date Column to Timeseries
In Ggplot2, What Do the End of the Boxplot Lines Represent
How to Document Data Sets with Roxygen
How to Clear Only a Few Specific Objects from the Workspace
Set R Plots X Axis to Show at Y=0
Combined Plot of Ggplot2 (Not in a Single Plot), Using Par() or Layout() Function