Home > Mobile >  mapping row values and column names in python pandas dataframe
mapping row values and column names in python pandas dataframe

Time:10-16

I get column names list from python pandas dataframe by columnvalues = List(df.columns.values) and row values by df.query('A=="foo"'). However, I will not require all cell values from all columns. I'd like to map or zip them as key(column name): value(cell value) for using separately as an output in an excel sheet.

columnvalues = List(df.columns.values)

['ColA','ColB','ColC','ColD','ColE']


rowData=df.loc[df['ColA']=='apple']

    ColA  ColB   ColC   ColD   ColE
13  apple NaN    height width  size

I have columnValues, but if I could also row values I can easily use dict(zip(colValues, rowValues)) method to create columnKey rowValue based dictionary then by calling dictionary to write output excel files. Because in Excel file which is output file, column numbers and column places differ from how they are set up in dataframe object.

Any ideas on how I can achieve this result, even with a different approach, would be greatly appreciated.

I need method to get this result below;

rowValuesList=['apple', NaN, 'height','width','size']

CodePudding user response:

We could do

rowValuesList = rowData.iloc[0].tolist()
  • Related