数据挖掘 pandas基础入门之选择

虽然标准的 Python/Numpy 的选择和设置表达式都能够直接派上用场,但是作为工程使用的代码,我们推荐使用经过优化的 pandas 数据访问方式:.at,.iat,.loc,.iloc和.ix。

获取

import pandas
import numpy

# 通过传递一个 numpyarray,时间索引以及列标签来创建一个DataFrame:
dates = pandas.date_range("20180509", periods=6)
df = pandas.DataFrame(numpy.random.randn(6, 4), index=dates, columns=list('ABCD'))
print("时间索引以及列标签来创建一个DataFrame:", df, sep="\n")

#  选择一个单独的列,这将会返回一个Series,等同于df.A
print("读取A列:", df['A'], sep="\n")  # Freq表示列,D表示日期

# 通过[]进行选择、切片
print("读取0到2行: ", df[0:3], sep="\n")
print("通过行名,读取指定行:", df['2018-05-09':'2018-05-11 '], sep="\n")
"E:\Python 3.6.2\python.exe" F:/PycharmProjects/test.py
时间索引以及列标签来创建一个DataFrame:
                   A         B         C         D
2018-05-09  0.019684  0.015831  0.014346 -0.612275
2018-05-10 -0.755389  0.290763 -1.175651  0.014877
2018-05-11 -0.004793 -1.615366  0.271680 -0.933383
2018-05-12 -1.229533 -0.222638  0.057783 -0.172315
2018-05-13  0.082170 -0.483592 -0.058921  0.081162
2018-05-14 -0.219111  1.482332  0.316326  0.243370
读取A列:
2018-05-09    0.019684
2018-05-10   -0.755389
2018-05-11   -0.004793
2018-05-12   -1.229533
2018-05-13    0.082170
2018-05-14   -0.219111
Freq: D, Name: A, dtype: float64
读取0到2行: 
                   A         B         C         D
2018-05-09  0.019684  0.015831  0.014346 -0.612275
2018-05-10 -0.755389  0.290763 -1.175651  0.014877
2018-05-11 -0.004793 -1.615366  0.271680 -0.933383
通过行名,读取指定行:
                   A         B         C         D
2018-05-09  0.019684  0.015831  0.014346 -0.612275
2018-05-10 -0.755389  0.290763 -1.175651  0.014877
2018-05-11 -0.004793 -1.615366  0.271680 -0.933383

Process finished with exit code 0

通过标签选择

import pandas
import numpy

# 通过传递一个 numpyarray,时间索引以及列标签来创建一个DataFrame:
dates = pandas.date_range("20180509", periods=6)
df = pandas.DataFrame(numpy.random.randn(6, 4), index=dates, columns=list('ABCD'))
print("时间索引以及列标签来创建一个DataFrame:", df, sep="\n")

# 使用标签来获取一个交叉区域
print("定位到dates[0],获取此行数据,包括列编号: ", df.loc[dates[0]], sep="\n")

# 通过标签来在多个轴上进行选择
print("获取此行数据第A,B列: ", df.loc[:, ['A', 'B']], sep="\n")

# 标签切片
print("定位到dates[2],获取此行数据第A,B列: ", df.loc[:dates[2], ['A', 'B']], sep="\n")

# 对于返回对象进行维度缩减(获取指定对象)
print("定位到dates[0],获取此行数据A, B列:",  df.loc[dates[0], ['A', 'B']], sep="\n")

# 获取一个标量
print("定位到dates[0],获取此行数据A列数据:", df.loc[dates[0], 'A'], sep="\n")

# 快速访问一个标量(与上述等价)
print("定位到dates[0],获取此行数据A列数据:", df.at[dates[0], 'A'])
"E:\Python 3.6.2\python.exe" F:/PycharmProjects/test.py
时间索引以及列标签来创建一个DataFrame:
                   A         B         C         D
2018-05-09 -0.271226  0.240662 -0.319637 -0.499980
2018-05-10 -1.943469 -0.503852 -0.139032 -0.504864
2018-05-11 -0.565398 -0.979566 -0.752174  1.565527
2018-05-12  0.562519 -0.608038 -0.756975  1.181634
2018-05-13 -0.196046 -0.055726  0.302884  0.811187
2018-05-14 -0.054767 -0.291400 -0.674922  0.325859
定位到dates[0],获取此行数据,包括列编号: 
A   -0.271226
B    0.240662
C   -0.319637
D   -0.499980
Name: 2018-05-09 00:00:00, dtype: float64
获取此行数据第A,B列: 
                   A         B
2018-05-09 -0.271226  0.240662
2018-05-10 -1.943469 -0.503852
2018-05-11 -0.565398 -0.979566
2018-05-12  0.562519 -0.608038
2018-05-13 -0.196046 -0.055726
2018-05-14 -0.054767 -0.291400
定位到dates[2],获取此行数据第A,B列: 
                   A         B
2018-05-09 -0.271226  0.240662
2018-05-10 -1.943469 -0.503852
2018-05-11 -0.565398 -0.979566
定位到dates[0],获取此行数据A, B列:
A   -0.271226
B    0.240662
Name: 2018-05-09 00:00:00, dtype: float64
定位到dates[0],获取此行数据A列数据:
-0.27122562914
定位到dates[0],获取此行数据A列数据: -0.27122562914

Process finished with exit code 0

猜你喜欢

转载自my.oschina.net/gain/blog/1821038