Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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不返回单个项目?_Python_Pandas - Fatal编程技术网

为什么Python不返回单个项目?

为什么Python不返回单个项目?,python,pandas,Python,Pandas,对于分配,我需要通过列名和索引访问CSV文件中的条目 我使用for循环来获取每个索引。与我的期望相反 read.loc[[i], ["magType"]] 返回类似于 而不仅仅是“mb”,这是我根据熊猫备忘单所期望的(链接如下) 为什么会这样?我如何才能只获取项目(在本例中为“mb”,没有magType和0) magType 0 mb read.loc[0,“magType”]返回0-第行和magType列的元素-可能是您想要的结果 另一方面,read.loc[[0],“magType”]返

对于分配,我需要通过列名和索引访问CSV文件中的条目

我使用for循环来获取每个索引。与我的期望相反

read.loc[[i], ["magType"]]
返回类似于

而不仅仅是“mb”,这是我根据熊猫备忘单所期望的(链接如下) 为什么会这样?我如何才能只获取项目(在本例中为“mb”,没有magType和0)

magType

0 mb

read.loc[0,“magType”]
返回
0
-第行和
magType
列的元素-可能是您想要的结果


另一方面,
read.loc[[0],“magType”]
返回数据帧的一部分,其中包含
[“magType”]
中的列,以及索引位于
[0]
中的所有行。(比较
read.loc[[1,2,3],“magType”,“magnize”]]
read.loc[[0:10],“magType”,“magnize”]]
以了解这种情况的一般情况。)与任何其他数据帧一样,它也有索引和列名。

我还要指出,按行/列访问标量标签的标准方法是
at
,即
read at[0,'magType']
。请注意“第
0
th行”的描述,只有当索引为常规
pd.RangeIndex
时,整数位置和标签之间的对齐才是正确的。
import pandas as pd
read = pd.read_csv("earthquake_data.csv")
print(read.loc[[0], ["magType"]])