Pandas - 获取给定列的第一行值

Ahm*_*que 236 python indexing head pandas

这似乎是一个非常简单的问题...但我没有看到我期待的简单答案.

那么,如何在Pandas的给定列的第n行获取值?(我对第一行特别感兴趣,但也会对更普遍的做法感兴趣).

例如,假设我想将Btime中的1.2值作为变量.

这是正确的方法吗?

df_test =

  ATime   X   Y   Z   Btime  C   D   E
0    1.2  2  15   2    1.2  12  25  12
1    1.4  3  12   1    1.3  13  22  11
2    1.5  1  10   6    1.4  11  20  16
3    1.6  2   9  10    1.7  12  29  12
4    1.9  1   1   9    1.9  11  21  19
5    2.0  0   0   0    2.0   8  10  11
6    2.4  0   0   0    2.4  10  12  15
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 371

要选择ith行,请使用iloc:

In [31]: df_test.iloc[0]
Out[31]: 
ATime     1.2
X         2.0
Y        15.0
Z         2.0
Btime     1.2
C        12.0
D        25.0
E        12.0
Name: 0, dtype: float64
Run Code Online (Sandbox Code Playgroud)

要选择Btime列中的第i个值,您可以使用:

In [30]: df_test['Btime'].iloc[0]
Out[30]: 1.2
Run Code Online (Sandbox Code Playgroud)

警告:我之前曾建议过df_test['Btime'].iloc[0].但是,这并不能保证在尝试按位置索引之前尝试按标签索引,因此无法为您提供df_test.iloc[0]['Btime']值.因此,如果DataFrame的整数索引不是从0开始的排序顺序,那么using 将返回标记的行而不是行.例如,df_test.iloc[0]['Btime']df_test['Btime'].iloc[0] df_test['Btime'].iloc[0] = xdf_test

df.iloc[0, df.columns.get_loc('Btime')] = x
Run Code Online (Sandbox Code Playgroud)


and*_*rew 25

请注意,@unutbu的答案将是正确的,直到您想要将值设置为新值,如果您的数据帧是视图,它将无法工作.

In [4]: df = pd.DataFrame({'foo':list('ABC')}, index=[0,2,1])
In [5]: df['bar'] = 100
In [6]: df['bar'].iloc[0] = 99
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.16.0_19_g8d2818e-py2.7-macosx-10.9-x86_64.egg/pandas/core/indexing.py:118: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
Run Code Online (Sandbox Code Playgroud)

另一种与设置和获取一致的方法是:

In [7]: df.loc[df.index[0], 'foo']
Out[7]: 'A'
In [8]: df.loc[df.index[0], 'bar'] = 99
In [9]: df
Out[9]:
  foo  bar
0   A   99
2   B  100
1   C  100
Run Code Online (Sandbox Code Playgroud)


Myk*_*tko 12

要访问单个值,您可以使用比iat以下方法快得多的iloc方法:

df['Btime'].iat[0]
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下方法take

df['Btime'].take(0)
Run Code Online (Sandbox Code Playgroud)


小智 11

  1. df.iloc[0].head(1) - 仅来自整个第一行的第一个数据集.
  2. df.iloc[0] - 列中的整个第一行.


Abd*_*res 11

另一种方法:

first_value = df['Btime'].values[0]
Run Code Online (Sandbox Code Playgroud)

这种方式似乎比使用更快.iloc:

In [1]: %timeit -n 1000 df['Btime'].values[20]
5.82 µs ± 142 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [2]: %timeit -n 1000 df['Btime'].iloc[20]
29.2 µs ± 1.28 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 11

.iat和是获取和设置单个值的方法,并且比和.at快得多。Mykola Zotko 在他们的回答中指出了这一点,但他们并没有充分利用。.iloc.loc.iat

当我们可以使用.iator时.at,我们只需要对数据帧进行索引一次。

这不太好:

df['Btime'].iat[0]
Run Code Online (Sandbox Code Playgroud)

这并不理想,因为“Btime”列首先被选为一个系列,然后.iat用于索引该系列。

这两个选项是最好的:

  1. 使用零索引位置:
    df['Btime'].iat[0]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用标签:
    df.iat[0, 4]  # get the value in the zeroth row, and 4th column
    
    Run Code Online (Sandbox Code Playgroud)

两种方法都返回值 1.2。


小智 7

通常,如果要从最佳方法的J列中拾取前N行,请执行以下操作:pandas dataframe

data = dataframe[0:N][:,J]
Run Code Online (Sandbox Code Playgroud)

  • @anis:为此,我相信您最好写一个新问题,寻求更一般的解决方案,然后自己回答。 (2认同)

Ale*_*ner 7

例如,要从“test”列和第 1 行获取值,其工作原理如下

df[['test']].values[0][0]
Run Code Online (Sandbox Code Playgroud)

因为只df[['test']].values[0]返回一个数组