Python Forum
How to find column index and its corresponding column name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find column index and its corresponding column name
#1
Hi,

I have below data:

import pandas as pd
import numpy as np
from scipy import stats
dataFileName='RFInput.xlsx'
sheetName='Rawdata'
sheetNamePara='paraList'
dataRaw=pd.read_excel(dataFileName, sheetname = sheetName)
datapara=pd.read_excel(dataFileName, sheetname = sheetNamePara)

noData=len(dataRaw)

labels = datapara
x = dataRaw[labels]

Rawdata:

A   B     C      D    E      F
0   1.2   1.6   3.2  3.2    1.6
1   1.2   1.6   3.2  3.2    1.6
2   2.6   1.9   6.5  6.5    1.9
0   1.2   1.6   3.2  3.2    1.6
1   2.6   1.9   6.5  6.5    1.9
4   1.2   1.6   3.2  3.2    1.6


paraList:
A   C  E  F
Y   N  Y  Y
I want to find column index of Y and N in paralist, and corresponding column names:

Y type column names are A, E, F, and its data in Rawdata,
data_Y:

A    E      F
0   3.2    1.6
1   3.2    1.6
2   6.5    1.9
0   3.2    1.6
1   6.5    1.9
4   3.2    1.6

data_N:
C  
1.6  
1.6   
1.9   
1.6  
1.9   
1.6
Reply
#2
The problem I see is that your paraList is not really a data frame as it has only one row... so looks like it is going to work better as a dictionary:
datapara.iloc[0].to_dict()
Output:
{'A': 'Y', 'C': 'N', 'E': 'Y', 'F': 'Y'}
Now it is easy to create a selector for the columns that has value 'Y'
selection = datapara.iloc[0].to_dict()
cols = [c for c in selection if selection[c] == 'Y']
print(dataRaw[cols])
Output:
A E F 0 0 3.2 1.6 1 1 3.2 1.6 2 2 6.5 1.9 3 0 3.2 1.6 4 1 6.5 1.9 5 4 3.2 1.6
To select the ones with 'N', you can use a similar process.
Remember also to add guards for the cases when a column is only in one of the tables... for example in this code a column only in datapara will raise an exception.
Reply
#3
(May-09-2018, 09:53 PM)killerrex Wrote: ....
Now it is easy to create a selector for the columns that has value 'Y'
selection = datapara.iloc[0].to_dict()
cols = [c for c in selection if selection[c] == 'Y']
print(dataRaw[cols])

is built on assumption that every key with value "Y" in selector corresponds to a column in dataRaw - which is in general is an unsafe assumption

cols = [c for c in daraRaw.columns if selection.get(c) == 'Y']
removes this problem
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Column Transformer with Mixed Types - sklearn aaldb 0 323 Feb-22-2024, 03:27 PM
Last Post: aaldb
  concat 3 columns of dataframe to one column flash77 2 839 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 1,021 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  attempt to split values from within a dataframe column mbrown009 8 2,340 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  Finding the median of a column in a huge CSV file markagregory 5 1,781 Jan-24-2023, 04:22 PM
Last Post: DeaD_EyE
  Make unique id in vectorized way based on text data column with similarity scoring ill8 0 887 Dec-12-2022, 03:22 AM
Last Post: ill8
  Impute 1 if previous row of 'days' column is between 0 & 7 JaneTan 2 1,085 Dec-08-2022, 07:42 PM
Last Post: deanhystad
  Increase df column values decimals SriRajesh 2 1,108 Nov-14-2022, 05:20 PM
Last Post: deanhystad
  pandas column percentile nuncio 7 2,434 Aug-10-2022, 04:41 AM
Last Post: nuncio
  how to expand each unique value in another column and fill zero if no match SriRajesh 0 833 Jul-10-2022, 09:21 AM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020