简体   繁体   English

根据行和列从pandas数据框中选择数据值以追加到列表

[英]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.

相关问题 根据约束从 Pandas DataFrame 中选择行 - Selecting row from a Pandas DataFrame based on constraints 根据存储在不同数据框中的行和列标签从熊猫数据框中选择值 - selecting values from a pandas dataframe based on row and column labels stored in a different dataframe 根据熊猫列中的值从DataFrame中选择特定的行 - Select particular row from a DataFrame based on value in a column in pandas 根据列值删除Pandas中的DataFrame行 - Deleting DataFrame row in Pandas based on column value 在 Pandas dataframe 中,如何根据每一行的值在 append 中创建一个新的 True / False 列? - In Pandas dataframe, how to append a new column of True / False based on each row's value? 根据计算值从Pandas DataFrame中的行中查找数据? - Find data from row in Pandas DataFrame based upon calculated value? 根据列中的值从 pandas dataframe 中的列表中删除元素 - Remove element from list in pandas dataframe based on value in column 根据数据框熊猫中的行值选择所有列 - Selecting all Columns based on Row value in a Dataframe Pandas 根据列从 Pandas 数据框中选择数据,该列由列表中的值组成? - Select data from pandas dataframe based on a column, consisting of values in a list? 熊猫根据索引向“数据框”列添加值,如果值存在,则追加 - Pandas Add value to Dataframe column based on index and if value exists then append
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM