Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否从DataFrame中提取包含.between()的行范围和特定列? 我刚刚被绊倒了:考虑这个例子: >>> import pandas as pd >>> df = pd.DataFrame({ "key":[1,3,6,10,15,21], "columnA":[10,20,30,40,50,60], "columnB":[100,200,300,400,500,600], "columnC":[110,202,330,404,550,606], }) >>> df key columnA columnB columnC 0 1 10 100 110 1 3 20 200 202 2 6 30 300 330 3 10 40 400 404 4 15 50 500 550 5 21 60 600 606_Python_Pandas_Dataframe - Fatal编程技术网

Python 是否从DataFrame中提取包含.between()的行范围和特定列? 我刚刚被绊倒了:考虑这个例子: >>> import pandas as pd >>> df = pd.DataFrame({ "key":[1,3,6,10,15,21], "columnA":[10,20,30,40,50,60], "columnB":[100,200,300,400,500,600], "columnC":[110,202,330,404,550,606], }) >>> df key columnA columnB columnC 0 1 10 100 110 1 3 20 200 202 2 6 30 300 330 3 10 40 400 404 4 15 50 500 550 5 21 60 600 606

Python 是否从DataFrame中提取包含.between()的行范围和特定列? 我刚刚被绊倒了:考虑这个例子: >>> import pandas as pd >>> df = pd.DataFrame({ "key":[1,3,6,10,15,21], "columnA":[10,20,30,40,50,60], "columnB":[100,200,300,400,500,600], "columnC":[110,202,330,404,550,606], }) >>> df key columnA columnB columnC 0 1 10 100 110 1 3 20 200 202 2 6 30 300 330 3 10 40 400 404 4 15 50 500 550 5 21 60 600 606,python,pandas,dataframe,Python,Pandas,Dataframe,所以,我想从这个表中提取数据,keycolumn(假设它是单调增长的)位于两个值(比如2和15)之间,但只针对一些特定的列(比如“columnA”和“columnC”)。希望这可以在一行中完成 现在,如果我想使用.between()方法,它基本上会为所有行返回true/false: >>> df['key'].between(2, 16) 0 False 1 True 2 True 3 True 4 True 5 False 因此,

所以,我想从这个表中提取数据,
key
column(假设它是单调增长的)位于两个值(比如2和15)之间,但只针对一些特定的列(比如“columnA”和“columnC”)。希望这可以在一行中完成

现在,如果我想使用
.between()
方法,它基本上会为所有行返回true/false:

>>> df['key'].between(2, 16)
0    False
1     True
2     True
3     True
4     True
5    False
因此,要真正提取这些行,我需要将上面的命令放在方括号中:

>>> df[df['key'].between(2, 16)]
   key  columnA  columnB  columnC
1    3       20      200      202
2    6       30      300      330
3   10       40      400      404
4   15       50      500      550
很好,这就是我需要的-我只需要限制列;因此我尝试以下方法:

>>> df[df['key'].between(2, 16), ["columnA"]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:/msys64/mingw64/lib/python3.8/site-packages/pandas/core/frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:/msys64/mingw64/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 116, in pandas._libs.index.IndexEngine.get_loc
TypeError: '(0    False
1     True
2     True
3     True
4     True
5    False
Name: key, dtype: bool, ['columnA'])' is an invalid key
>>df[df['key'].介于(2,16),[“columnA”]]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:/msys64/mingw64/lib/python3.8/site-packages/pandas/core/frame.py”,第2800行,在uu-getitem中__
indexer=self.columns.get_loc(键)
文件“C:/msys64/mingw64/lib/python3.8/site-packages/pandas/core/index/base.py”,第2646行,在get_loc中
返回发动机。获取位置(钥匙)
文件“pandas/_libs/index.pyx”,第111行,在pandas._libs.index.IndexEngine.get_loc中
文件“pandas/_libs/index.pyx”,第116行,在pandas._libs.index.IndexEngine.get_loc中
TypeError:'(0错误
1正确
2正确
3正确
4正确
5错误
Name:key,dtype:bool,['columnA'])是无效的键
呃…没有骰子


那么,我如何才能做到以上几点,并限制特定的列呢?

事实证明,我需要使用
.loc

>>> df.loc[df['key'].between(2, 16), ["columnA"]]
   columnA
1       20
2       30
3       40
4       50
…或者更确切地说,正如我最初想要的那样(并添加了“键”列):


您可以使用标准的数据帧切片方法:

df[df['key'].between(2,16)][['key','columnA','columnC']]

整洁-谢谢,@UJIN;熊猫语法有点让我困惑,所以我很难找到我应该如何准确地编写标准的切片方法
:)
df[df['key'].between(2,16)][['key','columnA','columnC']]