Python2.7的DataFrame中的.loc函数和Python3区别
在python2.7中之前用pandas中的DataFrame类型中,.loc表示标签索引, 其实语法没有变,报错的可能是你可能没有搞清楚loc在Series和DataFrame的区别:
loc在Series中直接取标签,loc在DataFrame中要先指定行然后再取标签,因为DataFrame有两个索引,行索引和列索引都要指定。
loc在Series用法
# 构建一个Series
series_obj = pd.Series(range(5), index=['A', 'B', 'C', 'D', 'E'])
print(series_obj)
"""
Series
A 0
B 1
C 2
D 3
E 4
dtype: int64
"""
print(series_obj.loc['A':'D'])
"""
A 0
B 1
C 2
D 3
dtype: int64
"""
print(series_obj.iloc[0:4])
"""
A 0
B 1
C 2
D 3
dtype: int64
"""
print(series_obj.ix[0:4])
"""
A 0
B 1
C 2
D 3
dtype: int64
"""
print(series_obj.ix['A':'D'])
"""
A 0
B 1
C 2
D 3
dtype: int64
"""
loc在DataFrame用法
# 构建DataFrame
df_obj2 = pd.DataFrame(dict_data)
print(df_obj2)
"""
A B C D E F
0 1 2021-04-01 1.0 3 python test
1 1 2021-04-01 1.0 3 java test
2 1 2021-04-01 1.0 3 c test
3 1 2021-04-01 1.0 3 nodejs test
"""
print(df_obj2.loc[1:3])
"""
A B C D E F
1 1 2021-04-01 1.0 3 java test
2 1 2021-04-01 1.0 3 c test
3 1 2021-04-01 1.0 3 nodejs test
"""
print(df_obj2.loc[1:3, 'A':'D'])
"""
A B C D
1 1 2021-04-01 1.0 3
2 1 2021-04-01 1.0 3
3 1 2021-04-01 1.0 3
"""
补充 三种索引方式
loc 标签索引
iloc (int loc)位置索引
lx 混合索引 先按默认标签查找 找不到再按位置索引查找
iloc在Series的用法 和loc差不多 只是不取尾
# 构建一个Series
series_obj = pd.Series(range(5), index=['A', 'B', 'C', 'D', 'E'])
print(series_obj)
"""
Series
A 0
B 1
C 2
D 3
E 4
dtype: int64
"""
print(series_obj.iloc[0:4])
"""
A 0
B 1
C 2
D 3
dtype: int64
"""
iloc在DataFrame的用法
print(df_obj2)
"""
A B C D E F
0 1 2021-04-01 1.0 3 python test
1 1 2021-04-01 1.0 3 java test
2 1 2021-04-01 1.0 3 c test
3 1 2021-04-01 1.0 3 nodejs test
"""
print(df_obj2.iloc[1:3])
"""
A B C D E F
1 1 2021-04-01 1.0 3 java test
2 1 2021-04-01 1.0 3 c test
"""
print(df_obj2.iloc[1:3, 2:5])
"""
C D E
1 1.0 3 java
2 1.0 3 c
"""
总结
- loc索引取头取尾 iloc取不到尾(标签索引报含头和尾)
- iloc不指定行和列,取所有行和列
- ix标签索引和位置索引都可以取到,一般先按标签索引取,再按位置索引
ix在Series用法:
# 构建一个Series
series_obj = pd.Series(range(5), index=['A', 'B', 'C', 'D', 'E'])
print(series_obj)
"""
Series
A 0
B 1
C 2
D 3
E 4
dtype: int64
"""
print(series_obj.ix[0:4])
"""
A 0
B 1
C 2
D 3
dtype: int64
"""
print(series_obj.ix['A':'D'])
"""
A 0
B 1
C 2
D 3
dtype: int64
"""
ix在DataFrame用法:
print(df_obj2)
"""
A B C D E F
0 1 2021-04-01 1.0 3 python test
1 1 2021-04-01 1.0 3 java test
2 1 2021-04-01 1.0 3 c test
3 1 2021-04-01 1.0 3 nodejs test
"""
# ix先把0当作标签索引去找 在不到再按照位置索引去找
print(df_obj2.ix[0:4, 0])
"""
0 1
1 1
2 1
3 1
"""
print(df_obj2.ix[0:4, 'A':'D'])
"""
A B C D
0 1 2021-04-01 1.0 3
1 1 2021-04-01 1.0 3
2 1 2021-04-01 1.0 3
3 1 2021-04-01 1.0 3
"""
print(df_obj2.ix[0:4, 0:4])
"""
A B C D
0 1 2021-04-01 1.0 3
1 1 2021-04-01 1.0 3
2 1 2021-04-01 1.0 3
3 1 2021-04-01 1.0 3
"""