在之前的pandas学习(一)中主要学习的是如何对列操作,它也可以对行进行操作。相当于列表中的索引。提取的是一个series。
>>> df.head()
Years rain_sep out_sep rain_feb out_feb rain_aug out_aug
0 1980/81 1182 5408 292 7248 174 2212
1 1981/82 1098 5112 257 7316 242 1936
2 1982/83 1156 5701 330 8567 124 1802
3 1983/84 993 4265 391 8905 141 1078
4 1984/85 1182 5364 217 5813 343 4313
>>> df.iloc[3]
Years 1983/84
rain_sep 993
out_sep 4265
rain_feb 391
out_feb 8905
rain_aug 141
out_aug 1078
Name: 3, dtype: object
>>> type(df.iloc[3])
<class 'pandas.core.series.Series'>
这种索引看起来并不直观,因为我们不可能记住每一行的行数,我们可以设置第一列的年份作为索引,之后索引的key由数字变成了字符串。
索引的时候由iloc变成了loc。iloc用于数值索引,loc用于字符串/数值索引。
>>> df = df.set_index(["Years"])
>>> df.head()
rain_sep out_sep rain_feb out_feb rain_aug out_aug
Years
1980/81 1182 5408 292 7248 174 2212
1981/82 1098 5112 257 7316 242 1936
1982/83 1156 5701 330 8567 124 1802
1983/84 993 4265 391 8905 141 1078
1984/85 1182 5364 217 5813 343 4313
>>> df.loc["1980/81"]
rain_sep 1182
out_sep 5408
rain_feb 292
out_feb 7248
rain_aug 174
out_aug 2212
Name: 1980/81, dtype: int64
还可以设置其他列作为索引。
>>> df = df.set_index(["rain_sep"])
>>> df.head()
out_sep rain_feb out_feb rain_aug out_aug
rain_sep
1182 5408 292 7248 174 2212
1098 5112 257 7316 242 1936
1156 5701 330 8567 124 1802
993 4265 391 8905 141 1078
1182 5364 217 5813 343 4313
>>> df.loc[1182]
out_sep rain_feb out_feb rain_aug out_aug
rain_sep
1182 5408 292 7248 174 2212
1182 5364 217 5813 343 4313
ix是另一个常用的引用一行的方法。那么,如果loc是字符串标签的索引方法,iloc是数字标签的索引方法,那什么是ix呢?事实上,ix是一个字符串标签的索引方法,但是它同样支持数字标签索引作为它的备选。
>>> df.ix[1182]
out_sep rain_feb out_feb rain_aug out_aug
rain_sep
1182 5408 292 7248 174 2212
1182 5364 217 5813 343 4313
好了,继续回到刚才的:df=df.reset_index("Years")
如果想回到初始的表格怎么办呢,需要set_index的逆操作reset_index即可。
>>> df = df.set_index(["Years"])
>>> df.head()
rain_sep out_sep rain_feb out_feb rain_aug out_aug
Years
1980/81 1182 5408 292 7248 174 2212
1981/82 1098 5112 257 7316 242 1936
1982/83 1156 5701 330 8567 124 1802
1983/84 993 4265 391 8905 141 1078
1984/85 1182 5364 217 5813 343 4313
>>> df = df.reset_index(["Years"])
>>> df.head()
Years rain_sep out_sep rain_feb out_feb rain_aug out_aug
0 1980/81 1182 5408 292 7248 174 2212
1 1981/82 1098 5112 257 7316 242 1936
2 1982/83 1156 5701 330 8567 124 1802
3 1983/84 993 4265 391 8905 141 1078
4 1984/85 1182 5364 217 5813 343 4313
排序
默认是升序,我用ascending=False将排序方式改成了降序。
>>> df.sort_index(ascending=False).head()
Years rain_sep out_sep rain_feb out_feb rain_aug out_aug
32 2012/13 1090 5329 350 9615 187 1797
31 2011/12 1285 5500 339 7630 379 5261
30 2010/11 1053 4521 265 6593 267 2885
29 2009/10 1103 4738 255 6435 244 1958
28 2008/09 1139 4941 268 6690 323 3189
应用函数
首先先定义一个函数
>>> def base_year(year):
... base_year=year[:4]
... base_year=pd.to_datetime(base_year).year
... return base_year
...
先来看一下to_datetime的用法,其实就是把给定的字符串变成时间格式。
参考:https://blog.csdn.net/lz_peter/article/details/78075909
>>> pd.to_datetime("1980").year
1980
>>> pd.to_datetime("1980")
Timestamp('1980-01-01 00:00:00')
>>> pd.to_datetime("1980/01/30")
Timestamp('1980-01-30 00:00:00')
>>> pd.to_datetime("1980/01/30 1:56:4")
Timestamp('1980-01-30 01:56:04')
将定义的函数应用在某一列上面,并且新增加一列。
>>> df["year"]=df.Years.apply(base_year)
>>> df.head()
Years rain_sep out_sep rain_feb out_feb rain_aug out_aug year
0 1980/81 1182 5408 292 7248 174 2212 1980
1 1981/82 1098 5112 257 7316 242 1936 1981
2 1982/83 1156 5701 330 8567 124 1802 1982
3 1983/84 993 4265 391 8905 141 1078 1983
4 1984/85 1182 5364 217 5813 343 4313 1984
将输入的dataframe分成几个组,并对组进行操作,比如min(),max(),mean()... ...
下面这个例子是将dataframe分成年代组,分成80年代,90年代... ...
注:取整除 - 返回商的整数部分(向下取整)
>>> df.groupby(df.year // 10 * 10 ).max()
Years rain_sep out_sep rain_feb out_feb rain_aug out_aug year
year
1980 1989/90 1210 5701 470 10520 343 4313 1989
1990 1999/00 1268 5824 484 11486 285 3206 1999
2000 2009/10 1387 6391 437 10926 357 5168 2009
2010 2012/13 1285 5500 350 9615 379 5261 2012
>>> df.groupby(df.year // 10 * 10 ).min()
Years rain_sep out_sep rain_feb out_feb rain_aug out_aug year
year
1980 1980/81 976 4265 217 5813 124 1078 1980
1990 1990/91 856 3479 245 5493 103 1231 1990
2000 2000/01 1021 4059 206 4578 176 1474 2000
2010 2010/11 1053 4521 265 6593 187 1797 2010
>>> df.groupby(df.year // 10 * 10 ).mean()
rain_sep out_sep rain_feb out_feb rain_aug out_aug year
year
1980 1,110 5,091 321 7,883 222 2,310 1,984
1990 1,112 4,906 339 8,078 215 2,061 1,994
2000 1,160 5,031 319 7,812 264 2,686 2,004
2010 1,143 5,117 318 7,946 278 3,314 2,011
还可以将dataframe分成两个组:
下面的例子是将dataframe分成两个组,一个分组是年代(80,90... ...),一个分组是rain_sep的下雨量是否大于1000(分成两组,一组为0,表示下雨量<1000,一组为1000,表示下雨量>=1000)
>>> df.groupby([df.year // 10 * 10 ,df.rain_sep // 1000 * 1000])[["out_sep","out_feb","out_aug"]].mean()
out_sep out_feb out_aug
year rain_sep
1980 0 4,298 7,685 1,259
1000 5,290 7,933 2,572
1990 0 3,479 5,515 1,439
1000 5,065 8,363 2,131
2000 1000 5,031 7,812 2,686
2010 1000 5,117 7,946 3,314
>>> df.groupby([df.year // 10 * 10 ,df.rain_sep // 1000 * 1000])[["out_sep","out_feb","out_aug","rain_aug"]].mean()
out_sep out_feb out_aug rain_aug
year rain_sep
1980 0 4,298 7,685 1,259 170
1000 5,290 7,933 2,572 235
1990 0 3,479 5,515 1,439 172
1000 5,065 8,363 2,131 219
2000 1000 5,031 7,812 2,686 264
2010 1000 5,117 7,946 3,314 278
>>> df.groupby([df.year // 10 * 10 ,df.rain_sep // 1000 * 1000]).mean()
rain_sep out_sep rain_feb out_feb rain_aug out_aug year
year rain_sep
1980 0 984 4,298 350 7,685 170 1,259 1,986
1000 1,142 5,290 314 7,933 235 2,572 1,984
1990 0 856 3,479 245 5,515 172 1,439 1,995
1000 1,140 5,065 349 8,363 219 2,131 1,994
2000 1000 1,160 5,031 319 7,812 264 2,686 2,004
2010 1000 1,143 5,117 318 7,946 278 3,314 2,011
为了之后的操作方便这对上面的结果进行赋值:
>>> decade_rain=df.groupby([df.year // 10 * 10 ,df.rain_sep // 1000 * 1000])["out_sep","out_feb","out_aug"].mean() >>> decade_rain
out_sep out_feb out_aug
year rain_sep
1980 0 4,298 7,685 1,259
1000 5,290 7,933 2,572
1990 0 3,479 5,515 1,439
1000 5,065 8,363 2,131
2000 1000 5,031 7,812 2,686
2010 1000 5,117 7,946 3,314
unstack操作的功能是将某一列前置成为行标签。下面看看它的实际效果。
>>> decade_rain
out_sep out_feb out_aug
year rain_sep
1980 0 4,298 7,685 1,259
1000 5,290 7,933 2,572
1990 0 3,479 5,515 1,439
1000 5,065 8,363 2,131
2000 1000 5,031 7,812 2,686
2010 1000 5,117 7,946 3,314
>>> decade_rain.unstack()
out_sep out_feb out_aug
rain_sep 0 1000 0 1000 0 1000
year
1980 4,298 5,290 7,685 7,933 1,259 2,572
1990 3,479 5,065 5,515 8,363 1,439 2,131
2000 nan 5,031 nan 7,812 nan 2,686
2010 nan 5,117 nan 7,946 nan 3,314
unstack默认是将第二列(1)变成行索引。
unstack()和unstack(1)得到的效果相同。
参考:http://www.bubuko.com/infodetail-2349978.html
>>> decade_rain.unstack(0)
out_sep out_feb out_aug \
year 1980 1990 2000 2010 1980 1990 2000 2010 1980 1990
rain_sep
0 4,298 3,479 nan nan 7,685 5,515 nan nan 1,259 1,439
1000 5,290 5,065 5,031 5,117 7,933 8,363 7,812 7,946 2,572 2,131
year 2000 2010
rain_sep
0 nan nan
1000 2,686 3,314
>>> decade_rain.unstack(1)
out_sep out_feb out_aug
rain_sep 0 1000 0 1000 0 1000
year
1980 4,298 5,290 7,685 7,933 1,259 2,572
1990 3,479 5,065 5,515 8,363 1,439 2,131
2000 nan 5,031 nan 7,812 nan 2,686
2010 nan 5,117 nan 7,946 nan 3,314