Python pandas,DataFrame切片,DataFrame取行取列,loc(),iloc()[亲测有效]

Python pandas,DataFrame切片,DataFrame取行取列,loc(),iloc()[亲测有效]demo.py(DataFrame切片,DataFrame取行取列):#coding=utf-8importnumpyasnpimportpandasaspd#创建DataFramedf=pd.DataFrame(np.arange(12,60).reshape((12,4)),columns=["WW","XX","YY","ZZ"])p…

大家好,欢迎来到IT知识分享网。

 

demo.py(DataFrame切片,DataFrame取行取列):

# coding=utf-8
import numpy as np
import pandas as pd


# 创建DataFrame
df = pd.DataFrame(np.arange(12, 60).reshape((12, 4)), columns=["WW", "XX", "YY", "ZZ"])
print(df)
'''
    WW  XX  YY  ZZ
0   12  13  14  15
1   16  17  18  19
2   20  21  22  23
3   24  25  26  27
4   28  29  30  31
5   32  33  34  35
6   36  37  38  39
7   40  41  42  43
8   44  45  46  47
9   48  49  50  51
10  52  53  54  55
11  56  57  58  59
'''

# pandas取行或者列的注意点
# - 方括号写数组,表示取行,对行进行操作
# - 写字符串,表示取列索引,对列进行操作


# 取行
print(df[:5])  # 取前5行
'''
   WW  XX  YY  ZZ
0  12  13  14  15
1  16  17  18  19
2  20  21  22  23
3  24  25  26  27
4  28  29  30  31
'''

# 取一列
print(df["YY"])
'''
0     14
1     18
2     22
3     26
4     30
5     34
6     38
7     42
8     46
9     50
10    54
11    58
Name: YY, dtype: int64
'''
print(type(df["YY"]))  # <class 'pandas.core.series.Series'>

# 取多列
print(df[["YY", "ZZ"]])
'''
    YY  ZZ
0   14  15
1   18  19
2   22  23
3   26  27
4   30  31
5   34  35
6   38  39
7   42  43
8   46  47
9   50  51
10  54  55
11  58  59
'''


# 取指定行的某一列
print(df[2:6]["ZZ"])
'''
2    23
3    27
4    31
5    35
Name: ZZ, dtype: int64
'''


# print(df.head(10))   # 取前10行,默认前5行
# print(df.tail(10))   # 取后10行,默认后5行


1.df.loc 通过索引标签名取数据

2.df.iloc 通过位置数据 (位置从0开始算)

 

demo.py(DataFrame,loc()根据索引名取行取列):

# coding=utf-8
import numpy as np
import pandas as pd


# 创建DataFrame
df = pd.DataFrame(np.arange(12, 32).reshape((5, 4)), index=["a","b","c","d","e"], columns=["WW","XX","YY","ZZ"])
print(df)
'''
   WW  XX  YY  ZZ
a  12  13  14  15
b  16  17  18  19
c  20  21  22  23
d  24  25  26  27
e  28  29  30  31
'''

# 取一行
print(df.loc["c"])  # Series类型
# print(df.loc["c", :])  # Series类型
'''
WW    20
XX    21
YY    22
ZZ    23
Name: c, dtype: int64
'''

# 取多行
print(df.loc[["a","d"]])  # DataFrame类型
'''
   WW  XX  YY  ZZ
a  12  13  14  15
d  24  25  26  27
'''

# 取连续多行
print(df.loc["b":"d"])  # loc()里的切片冒号,既包含"b"又包含"d"
'''
   WW  XX  YY  ZZ
b  16  17  18  19
c  20  21  22  23
d  24  25  26  27
'''

# 取单行单列
print(df.loc["b", "YY"])  # 18  类型:<class 'numpy.int64'>

# 取单行多列
print(df.loc["b", ["XX","ZZ"]])  # Series类型
'''
XX    17
ZZ    19
Name: b, dtype: int64
'''

# 取多行多列
print(df.loc[["b","d"], ["XX","ZZ"]])
'''
   XX  ZZ
b  17  19
d  25  27
'''

# 取连续多行多列
print(df.loc["b":"d", ["XX","ZZ"]])  # loc()里的切片冒号,既包含"b"又包含"d"
'''
   XX  ZZ
b  17  19
c  21  23
d  25  27
'''

# 取某一列
print(df.loc[:, ["XX"]])  # DataFrame类型
'''
   XX
a  13
b  17
c  21
d  25
e  29
'''

# 取某一列
print(df.loc[:, "XX"])  # Series类型
'''
a    13
b    17
c    21
d    25
e    29
Name: XX, dtype: int64
'''

demo.py(DataFrame,iloc()根据位置取行取列):

# coding=utf-8
import numpy as np
import pandas as pd


# 创建DataFrame
df = pd.DataFrame(np.arange(12, 32).reshape((5, 4)), index=["a","b","c","d","e"], columns=["WW","XX","YY","ZZ"])
print(df)
'''
   WW  XX  YY  ZZ
a  12  13  14  15
b  16  17  18  19
c  20  21  22  23
d  24  25  26  27
e  28  29  30  31
'''

# iloc()根据位置取 (位置从0开始算)
print(df.iloc[1:3,[2,3]])
'''
   YY  ZZ
b  18  19
c  22  23
'''


print(df.iloc[1:3,1:3])
'''
   XX  YY
b  17  18
c  21  22
'''


loc()和iloc()并不会拷贝,只是视图上的操作,通过loc()或iloc()进行赋值修改会直接修改原先的DataFrame。

demo.py(DataFrame,通过loc()或iloc()进行赋值修改会直接修改原先的DataFrame):

# coding=utf-8
import numpy as np
import pandas as pd


# 创建DataFrame
df = pd.DataFrame(np.arange(12, 32).reshape((5, 4)), index=["a","b","c","d","e"], columns=["WW","XX","YY","ZZ"])
print(df)
'''
   WW  XX  YY  ZZ
a  12  13  14  15
b  16  17  18  19
c  20  21  22  23
d  24  25  26  27
e  28  29  30  31
'''

# 通过loc()或iloc()会直接修改原DataFrame
df.loc["a":"c",["XX","YY"]] = 100
print(df)
'''
   WW   XX   YY  ZZ
a  12  100  100  15
b  16  100  100  19
c  20  100  100  23
d  24   25   26  27
e  28   29   30  31
'''

 

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/25075.html

(0)
上一篇 2023-08-03 21:00
下一篇 2023-08-04 14:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信