sir*_*ara 6 python pandas sklearn-pandas
我的数据是这样的:
[第一行是标题]
Name,Email,Age
Sachith,ko@gmail.com,23
Sim,sm@gmail.com,234
Yoshi,yosi@hotmail.com,2345
sarla,sarla@gmail.com,234
Run Code Online (Sandbox Code Playgroud)
我想访问元素,使行按标签指定为整数和列.即对SIM卡,我想访问它[1,"名称"]等上
我的问题是我应该使用loc还是ix?
看看文档,我对什么是熊猫索引感到困惑?是用于访问行还是列或两者?当我尝试打印这些数据的索引时,我得到一个(4,)dtype = int64数组[0,1,2,3].那么,列不是索引的一部分吗?
print (df)
Name Email Age
0 Sachith ko@gmail.com 23
1 Sim sm@gmail.com 234
2 Yoshi yosi@hotmail.com 2345
3 sarla sarla@gmail.com 234
#select by label 1 and label Name
a = df.loc[1, 'Name']
print (a)
Sim
Run Code Online (Sandbox Code Playgroud)
但是如果需要按位置(需要iloc)选择索引,按标签(需要loc)选择列:
df = df.set_index('Email')
print (df)
Name Age
Email
ko@gmail.com Sachith 23
sm@gmail.com Sim 234
yosi@hotmail.com Yoshi 2345
sarla@gmail.com sarla 234
Run Code Online (Sandbox Code Playgroud)
获取第二个索引的标签df.index[1]:
a = df.loc[df.index[1], 'Name']
print (a)
Sim
Run Code Online (Sandbox Code Playgroud)
或通过get_loc以下方式获得标签位置:
a = df.iloc[1, df.columns.get_loc('Name')]
print (a)
Sim
Run Code Online (Sandbox Code Playgroud)