Pandas学习,日积月累

  • 关于ix,iloc,loc:

loc函数: 完全基于标签位置的索引器,所谓标签位置就是下面定义的'row1','col2'等。primarily label based

iloc函数:完全基于index或者说integer的索引器。primarily integer position

通过以上两个函数并进行组合可以完成所有操作,ix为扩展,会经常报warning。

ix函数:supports mixed integer and label based access. It is primarily label based, but will fall back to integer positional access unless the corresponding axis is of integer type.

tips: 经过loc和iloc函数后的数据类型为series类型,与list类型相似,have the function of slice。不建议ix,warning影响美观

关于使用的实例:

import pandas as pd
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
rows = ['row1', 'row2', 'row3']  # 行标签
columns = ['col1', 'col2', 'col3']  # 列标签
df = pd.DataFrame(data, index=rows, columns=columns)
print(df)  # df: DataFrame type, use functions to access
print(df.loc['row1'])
print(df.loc['row1', 'col1'])  # based on label
print(df.loc['row1'][1])  # series type with slice function

print(df.iloc[0])
a = df.iloc[:, 1]  # a: series type have the function of [slice up]
print(df.iloc[:, 1])  # based on integer index
print(df.iloc[1]['col1'])  # combine with two methods

# not suggest, would have warning
print(df.ix[0][1])

# b type series  used double [[ && ]]
b = df.iloc[1][['col1','col2']]
# change types
print(list(b))
      col1  col2  col3
row1     1     2     3
row2     4     5     6
row3     7     8     9
col1    1
col2    2
col3    3
Name: row1, dtype: int64
1
col1    1
col2    2
col3    3
Name: row1, dtype: int64
row1    2
row2    5
row3    8
Name: col2, dtype: int64
4
2
[4, 5]

打不动字了,详细教程官方文档:https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-and-selecting-data

猜你喜欢

转载自blog.csdn.net/qq_40801709/article/details/100533109