- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章用pandas中的DataFrame时选取行或列的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import
numpy as np
import
pandas as pd
from
pandas
import
Sereis, DataFrame
ser
=
Series(np.arange(
3.
))
data
=
DataFrame(np.arange(
16
).reshape(
4
,
4
),index
=
list
(
'abcd'
),columns
=
list
(
'wxyz'
))
data[
'w'
]
#选择表格中的'w'列,使用类字典属性,返回的是Series类型
data.w
#选择表格中的'w'列,使用点属性,返回的是Series类型
data[[
'w'
]]
#选择表格中的'w'列,返回的是DataFrame属性
data[[
'w'
,
'z'
]]
#选择表格中的'w'、'z'列
data[
0
:
2
]
#返回第1行到第2行的所有行,前闭后开,包括前不包括后
data[
1
:
2
]
#返回第2行,从0计,返回的是单行,通过有前后值的索引形式,
#如果采用data[1]则报错
data.ix[
1
:
2
]
#返回第2行的第三种方法,返回的是DataFrame,跟data[1:2]同
data[
'a'
:
'b'
]
#利用index值进行切片,返回的是**前闭后闭**的DataFrame,
#即末端是包含的
data.irow(
0
)
#取data的第一行
data.icol(
0
)
#取data的第一列
data.head()
#返回data的前几行数据,默认为前五行,需要前十行则dta.head(10)
data.tail()
#返回data的后几行数据,默认为后五行,需要后十行则data.tail(10)
ser.iget_value(
0
)
#选取ser序列中的第一个
ser.iget_value(
-
1
)
#选取ser序列中的最后一个,这种轴索引包含索引器的series不能采用ser[-1]去获取最后一个,这回引起歧义。
data.iloc[
-
1
]
#选取DataFrame最后一行,返回的是Series
data.iloc[
-
1
:]
#选取DataFrame最后一行,返回的是DataFrame
data.loc[
'a'
,[
'w'
,
'x'
]]
#返回‘a'行'w'、'x'列,这种用于选取行索引列索引已知
data.iat[
1
,
1
]
#选取第二行第二列,用于已知行、列位置的选取。
|
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
import
pandas as pd
from
pandas
import
Series, DataFrame
import
numpy as np
data
=
DataFrame(np.arange(
15
).reshape(
3
,
5
),index
=
[
'one'
,
'two'
,
'three'
],columns
=
[
'a'
,
'b'
,
'c'
,
'd'
,
'e'
])
data
Out[
7
]:
a b c d e
one
0
1
2
3
4
two
5
6
7
8
9
three
10
11
12
13
14
#对列的操作方法有如下几种
data.icol(
0
)
#选取第一列
E:\Anaconda2\lib\site
-
packages\spyder\utils\ipython\start_kernel.py:
1
: FutureWarning: icol(i)
is
deprecated. Please use .iloc[:,i]
# -*- coding: utf-8 -*-
Out[
35
]:
one
0
two
5
three
10
Name: a, dtype: int32
data[
'a'
]
Out[
8
]:
one
0
two
5
three
10
Name: a, dtype: int32
data.a
Out[
9
]:
one
0
two
5
three
10
Name: a, dtype: int32
data[[
'a'
]]
Out[
10
]:
a
one
0
two
5
three
10
data.ix[:,[
0
,
1
,
2
]]
#不知道列名只知道列的位置时
Out[
13
]:
a b c
one
0
1
2
two
5
6
7
three
10
11
12
data.ix[
1
,[
0
]]
#选择第2行第1列的值
Out[
14
]:
a
5
Name: two, dtype: int32
data.ix[[
1
,
2
],[
0
]]
#选择第2,3行第1列的值
Out[
15
]:
a
two
5
three
10
data.ix[
1
:
3
,[
0
,
2
]]
#选择第2-4行第1、3列的值
Out[
17
]:
a c
two
5
7
three
10
12
data.ix[
1
:
2
,
2
:
4
]
#选择第2-3行,3-5(不包括5)列的值
Out[
29
]:
c d
two
7
8
data.ix[data.a>
5
,
3
]
Out[
30
]:
three
13
Name: d, dtype: int32
data.ix[data.b>
6
,
3
:
4
]
#选择'b'列中大于6所在的行中的第4列,有点拗口
Out[
31
]:
d
three
13
data.ix[data.a>
5
,
2
:
4
]
#选择'a'列中大于5所在的行中的第3-5(不包括5)列
Out[
32
]:
c d
three
12
13
data.ix[data.a>
5
,[
2
,
2
,
2
]]
#选择'a'列中大于5所在的行中的第2列并重复3次
Out[
33
]:
c c c
three
12
12
12
#还可以行数或列数跟行名列名混着用
data.ix[
1
:
3
,[
'a'
,
'e'
]]
Out[
24
]:
a e
two
5
9
three
10
14
data.ix[
'one'
:
'two'
,[
2
,
1
]]
Out[
25
]:
c b
one
2
1
two
7
6
data.ix[[
'one'
,
'three'
],[
2
,
2
]]
Out[
26
]:
c c
one
2
2
three
12
12
data.ix[
'one'
:
'three'
,[
'a'
,
'c'
]]
Out[
27
]:
a c
one
0
2
two
5
7
three
10
12
data.ix[[
'one'
,
'one'
],[
'a'
,
'e'
,
'd'
,
'd'
,
'd'
]]
Out[
28
]:
a e d d d
one
0
4
3
3
3
one
0
4
3
3
3
#对行的操作有如下几种:
data[
1
:
2
]
#(不知道列索引时)选择第2行,不能用data[1],可以用data.ix[1]
Out[
18
]:
a b c d e
two
5
6
7
8
9
data.irow(
1
)
#选取第二行
E:\Anaconda2\lib\site
-
packages\spyder\utils\ipython\start_kernel.py:
1
: FutureWarning: irow(i)
is
deprecated. Please use .iloc[i]
# -*- coding: utf-8 -*-
Out[
36
]:
a
5
b
6
c
7
d
8
e
9
Name: two, dtype: int32
data.ix[
1
]
#选择第2行
Out[
20
]:
a
5
b
6
c
7
d
8
e
9
Name: two, dtype: int32
data[
'one'
:
'two'
]
#当用已知的行索引时为前闭后闭区间,这点与切片稍有不同。
Out[
22
]:
a b c d e
one
0
1
2
3
4
two
5
6
7
8
9
data.ix[
1
:
3
]
#选择第2到4行,不包括第4行,即前闭后开区间。
Out[
23
]:
a b c d e
two
5
6
7
8
9
three
10
11
12
13
14
data.ix[
-
1
:]
#取DataFrame中最后一行,返回的是DataFrame类型,**注意**这种取法是有使用条件的,只有当行索引不是数字索引时才可以使用,否则可以选用`data[-1:]`--返回DataFrame类型或`data.irow(-1)`--返回Series类型
Out[
11
]:
a b c d e
three
10
11
12
13
14
data[
-
1
:]
#跟上面一样,取DataFrame中最后一行,返回的是DataFrame类型
Out[
12
]:
a b c d e
three
10
11
12
13
14
data.ix[
-
1
]
#取DataFrame中最后一行,返回的是Series类型,这个一样,行索引不能是数字时才可以使用
Out[
13
]:
a
10
b
11
c
12
d
13
e
14
Name: three, dtype: int32
data.tail(
1
)
#返回DataFrame中的最后一行
data.head(
1
)
#返回DataFrame中的第一行
|
最近处理数据时发现当pd.read_csv()数据时有时候会有读取到未命名的列,且该列也用不到,一般是索引列被换掉后导致的,有强迫症的看着难受,这时候dataframe.drop([columns,])是没法处理的,怎么办呢, 。
最笨的方法是直接给列索引重命名:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
data6
Unnamed:
0
high symbol time
date
2016
-
11
-
01
0
3317.4
IF1611
18
:
10
:
44.8
2016
-
11
-
01
1
3317.4
IF1611
06
:
01
:
04.5
2016
-
11
-
01
2
3317.4
IF1611
07
:
46
:
25.5
2016
-
11
-
01
3
3318.4
IF1611
09
:
30
:
04.0
2016
-
11
-
01
4
3321.8
IF1611
09
:
31
:
04.0
data6.columns
=
list
(
'abcd'
)
data6
a b c d
date
2016
-
11
-
01
0
3317.4
IF1611
18
:
10
:
44.8
2016
-
11
-
01
1
3317.4
IF1611
06
:
01
:
04.5
2016
-
11
-
01
2
3317.4
IF1611
07
:
46
:
25.5
2016
-
11
-
01
3
3318.4
IF1611
09
:
30
:
04.0
2016
-
11
-
01
4
3321.8
IF1611
09
:
31
:
04.0
|
以上这篇用pandas中的DataFrame时选取行或列的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:http://bbs.fishc.com/thread-79821-1-1.html 。
最后此篇关于用pandas中的DataFrame时选取行或列的方法的文章就讲到这里了,如果你想了解更多关于用pandas中的DataFrame时选取行或列的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!