Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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 熊猫:将列表转换为int64index_Python_List_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 熊猫:将列表转换为int64index

Python 熊猫:将列表转换为int64index,python,list,python-3.x,pandas,dataframe,Python,List,Python 3.x,Pandas,Dataframe,假设我在pandas中有一个数据帧df,行为0到99,我只想保留列表x中的df行 假设listx有一些值,比如基于一些可以更改的编程逻辑的[3,10,71] 如果我尝试访问df[df.index[x],'SomeColName']我通常会收到一个错误,说TypeError:unhable type:'Int64Index' 如何将列表转换为int64索引类型 我使用的是Python 3.4。这里不需要将列表转换为索引-如果您想访问特定的行和列,可以使用iloc: df.iloc[[3, 10,

假设我在pandas中有一个数据帧
df
,行为0到99,我只想保留列表
x
中的
df

假设list
x
有一些值,比如基于一些可以更改的编程逻辑的
[3,10,71]

如果我尝试访问
df[df.index[x],'SomeColName']
我通常会收到一个错误,说
TypeError:unhable type:'Int64Index'

如何将
列表
转换为
int64索引
类型


我使用的是Python 3.4。

这里不需要将列表转换为索引-如果您想访问特定的行和列,可以使用
iloc

df.iloc[[3, 10, 71], 'SomeColName']
这将返回“SomeColName”列第3、10和71行的数据帧视图

如果要删除所有其他行和列,可以将名称
df
重新指定给此视图


编写
df[df.index[x],df.index[x]
实际上试图找到两个名为
df.index[x]
'SomeColName'
的列。由于列名需要可散列,熊猫在查找
df.index[x]
时会出现一个错误(索引对象是可变的)。

实际上,在
df.loc[[3,10,71],'SomeColName']
当行标记为
3、10和71
而不是每个seAh的行
3、10和71时,您的答案会返回
SomeColName
中的值,如果您想要
iloc
(而不是
loc
)-我已经编辑过了。