简体   繁体   中英

Selecting a data value from pandas dataframe based on row and column to append to list

I have a PandasData Frame that is 26 columns and 100 rows. I want to extract a particular value from column 25 (which is called Unnamed: 24) row 50 and throw it into a list. Is there any way to do this? My columns are called Unnamed: 0, Unnamed: 1, ..., Unnamed: 25; and the rows are just going 0 to 99:

     Unnamed 0:   .....     Unnamed: 24     Unnamed: 25
  0
  1
  .
  .
  50                              50
  .
  .
  99

and

Numbers = []

I want to append this value 50 to Numbers which is from column 24 row 50.

My data frame is x = xls.parse('excelfile1.xls'), I am parsing a dataframe from an excel spreadsheet

You can use iloc for this:

Numbers = []
value =  df1.iloc[24,50]
Numbers.append(value)

Or as a more general example:

import pandas as pd
import numpy as np

df = pd.DataFrame(index=range(0,5), data=[range(5*i,5*i+5) for i in range(0,5)])

df :

    0   1   2   3   4
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19
4  20  21  22  23  24

and print df.iloc[2,2] returning 12

For selecting a single value from a DataFrame or Series, at (label based scalar indexing) and iat (index based scalar indexing) are generally the fastest.

numbers = []
numbers.append(df.iat(50, 24))

Lets say you had three pairs of numbers representing row and column index values where you want to lookup a value from your DataFrame. You could efficiently accomplish this goal as follows:

pairs = [(10, 20), (20, 25), (30, 30)]
[numbers.append(df.iat(row, col)) for row, col in pairs]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM