Summary of Indexing operation in DataFrame of Pandas
For new users of pandas, the index of DataFrame may seem confusing, so personally I list all its usage in detail and finally make a conclusion about the result of exploration on indexing operation on DataFrame of pandas.
when val is a list,df[val] selects sequence columns from DataFrame,returnning DataFrame type.
df[['one','two']]
one
two
Ohio
0
1
Colorado
4
5
Utah
8
9
New York
12
13
when val is :num, df[val] selects rows, and that is for a convenience purpose.That is equivalent to df.iloc[:num],which is specially used to deal with row selection.
df[:2]
one
two
three
four
Ohio
0
1
2
3
Colorado
4
5
6
7
df[val],when val is pd.Series whose index is the same with df,value is boolean,returns the index whose value in pd.Series is True.In this case,pd.DataFrame.any or pd.DataFrame.all always returns this kind of pd.Series as the input of val in df[val] for the purpose of filtering.
df.iloc[:2] # the same with above
one
two
three
four
Ohio
0
1
2
3
Colorado
4
5
6
7
df[1:3]
one
two
three
four
Colorado
4
5
6
7
Utah
8
9
10
11
df.iloc[1:3]
one
two
three
four
Colorado
4
5
6
7
Utah
8
9
10
11
when val is boolean DataFrame, df[val] sets values based on boolean
when val is a single index value,selects corresponding row,returnning Series type, and when val is list of index vale, selects corresponding rows,returnning DataFrame type.
when val is a single column value,selects corresponding column,returning Series type and when val is list of columns,select corresponding columns,returnning DataFrame type.
when val1 may be a single index value or list of index values,and val2 may be a single column value or list of column values,selects the combination data decided by both val1 and val2.And specially, val1 or val2 can both be : to participate in the combination.
The same with df.at,except val1 and val2 shall be both integer
df.iat[2,2]
10
df
one
two
three
four
Ohio
0
0
0
0
Colorado
0
5
6
7
Utah
8
9
10
11
New York
12
13
14
15
Conclusion
val in df[val] can be a column value or list of column values in this case to selecting the whole column,and specially can also be set :val meaning to select corresponding sliced rows.And also can be boolean DataFrame to set values.
Generally speaking, df.loc[val] is mainly used to select rows or the combination of rows and columns,so val has the following forms:single row value,list of row values,val1,val2(val1 and val2 can be single value or list of values or :,and in this form,it selects the combination index value val1 and column value val2
df.iloc[val] is the same with df.loc,except val demands integer,whatever single integer value or lists of integers.
df.at[val1,val2] shall be only single value and this also applies to df.iat[val1,val2]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix