[英]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. 我有一个PandasData框架,它是26列100行。 I want to extract a particular value from column 25 (which is called Unnamed: 24) row 50 and throw it into a list.
我想从第25列(称为Unnamed:24)第50行中提取特定值,并将其放入列表中。 Is there any way to do this?
有什么办法吗? My columns are called Unnamed: 0, Unnamed: 1, ..., Unnamed: 25;
我的列称为“未命名:0”,“未命名:1,...,未命名:25; and the rows are just going 0 to 99:
行仅从0到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. 我想将此值50附加到来自第24列第50行的Numbers中。
My data frame is x = xls.parse('excelfile1.xls'), I am parsing a dataframe from an excel spreadsheet 我的数据框是x = xls.parse('excelfile1.xls'),我是从Excel电子表格中解析数据框
You can use iloc
for this: 您可以为此使用
iloc
:
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
: 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
并打印
df.iloc[2,2]
返回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. 为了从DataFrame或Series中选择单个值,
at
(基于标签的标量索引)和iat
(基于索引的标量索引)通常是最快的。
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. 假设您有三对数字,分别代表要从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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.