简体   繁体   English

Python-无效的语法错误

[英]Python - invalid syntax error

I am learning python , and I am running below code : 我正在学习python,并且正在运行以下代码:

df1 = pd.DataFrame( [['A',20,30],['B',30,40]],index=['A1','B1'],columns = ['a','b','c'])

df1.loc['A1':'B1',['a':'b']] #edit to df1 to make easier for answerers

It is giving syntax error : invalid syntax (at ':' after 'a') 它给出语法错误:语法无效(在“ a”之后的“:”处)

Whats wrong with this? 这怎么了

You want to give it a slice object, as per the documentation here . 您要根据此处文档为其指定切片对象。 So df1.loc['A1':'B1','a':'b'] works. 因此df1.loc['A1':'B1','a':'b']有效。 Note that you want df1. 请注意,您需要df1. not df. 不是df. .

Allowed inputs are: 允许的输入为:

  • A single label, eg 5 or 'a', (note that 5 is interpreted as a label of the >index, and never as an integer position along the index). 单个标签,例如5或'a'(请注意,5被解释为> index的标签,而从不解释为沿索引的整数位置)。
  • A list or array of labels, eg ['a', 'b', 'c'] 标签列表或数组,例如['a','b','c']
  • A slice object with labels, eg 'a':'f'. 带标签的切片对象,例如'a':'f'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM