Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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_Dataframe_Int_Loc - Fatal编程技术网

Python 按索引名调用数据帧值

Python 按索引名调用数据帧值,python,pandas,dataframe,int,loc,Python,Pandas,Dataframe,Int,Loc,我有这个数据框: index name a b 1-1 1 2 1-2 2 4 5-1 3 6 5-2 4 8 7-1 5 4 7-2 6 5 我只想调用以'index name'=5开头的值 index name a b 5-1 3 6 5-2 4 8 我试过: df = df.loc['index name'] == 5 但

我有这个数据框:

index
name    a     b
1-1     1     2
1-2     2     4
5-1     3     6
5-2     4     8
7-1     5     4
7-2     6     5
我只想调用以'index name'=5开头的值

index
name     a     b
5-1      3     6
5-2      4     8
我试过:

df = df.loc['index name'] == 5 
但我得到了SyntaxError:无效令牌

有什么建议吗?谢谢

编辑

如果“索引名”是一列,则它可用于:

df = df.loc[df['your column'] == 5]
df.loc[df.index.str.startswith('5')]
但是,如果“索引名”是一个索引,则它不起作用

编辑2

如果“索引名”是索引,则它可用于:

df = df.loc[df['your column'] == 5]
df.loc[df.index.str.startswith('5')]

谢谢,如果“索引名”是一列,那么这个就有效了。但是如果'index name'是一个索引,它就不起作用了。我仍然得到了@Jack\T如果你的列是一个索引,请尝试:
df.loc[df.index.str.startswith(“5”)]
谢谢!但在更改“索引名称”时还有另一个问题。请看编辑2,谢谢@杰克,也许可以试试
df.loc[df.index.str.startswith(“5 | 7”)]
df.loc[df.index.str.contains(^5 | ^7”)]
?谢谢,如果“index name”是一列,这个就行了。但是如果'index name'是一个索引,它就不起作用了。我仍然得到了@Jack\T如果你的列是一个索引,请尝试:
df.loc[df.index.str.startswith(“5”)]
谢谢!但在更改“索引名称”时还有另一个问题。请看编辑2,谢谢@杰克,也许可以试试
df.loc[df.index.str.startswith(“5 | 7”)]
df.loc[df.index.str.contains(“^5 | ^7”)]