Solerek
Solerek

Reputation: 19

Create Column in Dataframe Passing Values from Columns for .iloc in Different Dataframe

I am looking to convert some Excel spreadsheets into a different format and so I have a Dataframe where I have converted the cell position location into integer locations which can be used for .iloc.

Example:

df
Col Row
 1   2
 5   6
 9   6

And so on. The other Dataframe is just a parsed Excel sheet loaded using pd.ExcelFile, so it is returning the the info in the original cell locations without issue (I checked it by exporting a .csv).

I want to create a new column ['Info'] by passing the values for each row of the original Dataframe into a .iloc of the second but my code returns no results.

df['Info'] = df.apply(excel_sheet.iloc[df['Row'], df['Col']])

How do I pass the info from each row to apply into a different Dataframe and return the "cell" info from the Excel sheet?

Upvotes: 1

Views: 37

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

Since these are positional indexing, you can try numpy:

df['Info'] = excel_sheet.to_numpy()[df['Row'], df['Col']]

Upvotes: 1

Related Questions