简体   繁体   English

从 pandas DataFrame 中选择数据

[英]Selecting data from a pandas DataFrame

I have defined a pandas DataFrame, given the number of rows (index) and columns.给定行数(索引)和列数,我定义了一个 pandas DataFrame。 I perform a series of operations and store the data in such DataFrame. The code that makes this operation is the next one:我进行了一系列操作,将数据存储在这样的DataFrame中。进行这个操作的代码是下一个:

import math
import numpy as np
import pandas as pd

sens_fac = [0.8, 1, 1.2]

A = 13;
B = 5;
C = 7/2;
D = 3*1.2;
par = [A,B,C,D]
data = pd.DataFrame(index=range(len(sens_fac)),columns=range(len(par)))

for i in range(len(par)):
    factors = [1, 1, 1, 1]
    for j in range(len(sens_fac)):
        factors[i] = sens_fac[j]
        print(factors)
        x=25
        t1 = np.log(x)**math.sin(x/(A*factors[0]))
        t2 = (B*factors[1])*math.sqrt(x)
        t3 = (factors[2]*C)**math.exp(1/x)
        t4 = x/(factors[3]*D)*2
        res = t1 + t2 + t3 + t4
        data[i][j] = res

The problem is when I try to select a specific element of such DataFrame. For example, if I print data, it would be a DataFrame of 3 rows and 4 columns .问题是当我尝试 select 此类 DataFrame 的特定元素时。例如,如果我打印数据,它将是 DataFrame 的3 行和 4 列 But when I try to select the element of the third row and fourth column ( data[2][3] ), I get an error.但是当我尝试 select 第三行第四列( data[2][3] )的元素时,出现错误。 On the other hand, if I select the element as data[3][2] , it gives me the number I am looking for, but I understand that data[3][2] would be the fourth row and third column, which is an element that does not exist.另一方面,如果我 select 元素作为data[3][2] ,它给了我我正在寻找的数字,但我知道data[3][2]将是第四行和第三列,这是一个不存在的元素。

The data frame values can be accessed using explicit indexing(loc), implicit indexing (iloc).可以使用显式索引 (loc)、隐式索引 (iloc) 访问数据框值。 To be more clear: suppose column 3 has the name 'qwe', and the index of row 2 will be 'c'.更清楚一点:假设第 3 列的名称为“qwe”,第 2 行的索引将为“c”。 This is called explicit reference to indexes.这称为对索引的显式引用。

data.loc['c', 'qwe']

Implicitly, you can apply like this:隐含地,您可以这样申请:

data.iloc[2, 3]

But, if you will use a slice.但是,如果您将使用切片。 Then with explicit indexing, there will be one more value, since with explicit indexing, the slice occurs inclusive.然后使用显式索引,将多一个值,因为使用显式索引,切片包含在内。

print(data.loc[:2, 3])
print(data.iloc[:2, 3])
"""
0    49.0406
1    45.5684
2    43.2536
Name: 3, dtype: object
0    49.0406
1    45.5684
Name: 3, dtype: object
"""

Since you have indexes (rows) in strict numbering, using an explicit and implicit index will lead to the same result.由于您有严格编号的索引(行),使用显式和隐式索引将导致相同的结果。

print(data.loc[2, 3])#43.2535547215997
print(data.iloc[2, 3])#43.2535547215997

iloc, loc and at, iat the difference is described here iloc、loc和at、iat的区别在这里说明

For single values, it is assumed that faster at, iat.对于单个值,假设更快,iat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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