【pandas】17 数据处理和绘图
2023.1.16 pandas数据处理方法和绘图:读取数据、更改数据、时间数据等
主要参考:https://mofanpy.com/tutorials/data-manipulation/pandas/time
17.1运算方法
17.1.1 筛选赋值运算
- 就是用前面的方法对数据进行筛选,然后对值进行一定的操作(对数据修改)
本章实例就是通过不同方法筛选出需要的位置,进行赋值
import pandas as pd
import numpy as np
data = np.arange(-12, 12).reshape((6, 4))
df = pd.DataFrame(
data,
index=list("abcdef"),
columns=list("ABCD"))
df
|
A |
B |
C |
D |
a |
-12 |
-11 |
-10 |
-9 |
b |
-8 |
-7 |
-6 |
-5 |
c |
-4 |
-3 |
-2 |
-1 |
d |
0 |
1 |
2 |
3 |
e |
4 |
5 |
6 |
7 |
f |
8 |
9 |
10 |
11 |
df["A"] *= 0
df
|
A |
B |
C |
D |
a |
0 |
-11 |
-10 |
-9 |
b |
0 |
-7 |
-6 |
-5 |
c |
0 |
-3 |
-2 |
-1 |
d |
0 |
1 |
2 |
3 |
e |
0 |
5 |
6 |
7 |
f |
0 |
9 |
10 |
11 |
df.loc['a','A']=100
df.iloc[1,0]=200
df
|
A |
B |
C |
D |
a |
100 |
-11 |
-10 |
-9 |
b |
200 |
-7 |
-6 |
-5 |
c |
0 |
-3 |
-2 |
-1 |
d |
0 |
1 |
2 |
3 |
e |
0 |
5 |
6 |
7 |
f |
0 |
9 |
10 |
11 |
df.iloc[0,:] *= 2
df
|
A |
B |
C |
D |
a |
200 |
-22 |
-20 |
-18 |
b |
200 |
-7 |
-6 |
-5 |
c |
0 |
-3 |
-2 |
-1 |
d |
0 |
1 |
2 |
3 |
e |
0 |
5 |
6 |
7 |
f |
0 |
9 |
10 |
11 |
df["A"][df["A"]==0]=1
df
|
A |
B |
C |
D |
a |
200 |
-22 |
-20 |
-18 |
b |
200 |
-7 |
-6 |
-5 |
c |
1 |
-3 |
-2 |
-1 |
d |
1 |
1 |
2 |
3 |
e |
1 |
5 |
6 |
7 |
f |
1 |
9 |
10 |
11 |
17.1.2 apply
数据.apply(方程式,。。)
:通过方程式的筛选选择,批量修改
df
|
A |
B |
C |
D |
a |
200 |
-22 |
-20 |
-18 |
b |
200 |
-7 |
-6 |
-5 |
c |
1 |
-3 |
-2 |
-1 |
d |
1 |
1 |
2 |
3 |
e |
1 |
5 |
6 |
7 |
f |
1 |
9 |
10 |
11 |
df.apply(np.sqrt)
|
A |
B |
C |
D |
a |
14.142136 |
NaN |
NaN |
NaN |
b |
14.142136 |
NaN |
NaN |
NaN |
c |
1.000000 |
NaN |
NaN |
NaN |
d |
1.000000 |
1.000000 |
1.414214 |
1.732051 |
e |
1.000000 |
2.236068 |
2.449490 |
2.645751 |
f |
1.000000 |
3.000000 |
3.162278 |
3.316625 |
def func(x):
return x[0] * 2, x[1] * -1
df.apply(func, axis=1, result_type='expand')
|
0 |
1 |
a |
400 |
22 |
b |
400 |
7 |
c |
2 |
3 |
d |
2 |
-1 |
e |
2 |
-5 |
f |
2 |
-9 |
df.iloc[0]
A 200
B -22
C -20
D -18
Name: a, dtype: int32
result_type='expand'
:让输出的结果可以生成多 column。如下:
def func(x):
return x[0] * 2, x[1] * -1
df.apply(func, axis=1)
a (400, 22)
b (400, 7)
c (2, 3)
d (2, -1)
e (2, -5)
f (2, -9)
dtype: object
- 如果
reult_type="broadcast"
,那么原 column 和 index 名会继承到新生成的数据中;没有的话本例column
是0-3
- 因为广播,这个return必须凑齐colunm个数,即使没变化,也要写上,不然报错
print("df:\n",df)
def func(x):
return x[0] * 2, x[1] * -1,x[2] ,x[3]
print("-----")
print(df.apply(func, axis=1, result_type='expand'))
df.apply(func, axis=1, result_type='broadcast')
df:
A B C D
a 200 -22 -20 -18
b 200 -7 -6