以同时处理两列数据为例,将两列中的数据相加,生成另外一列:
复制代码
1
2
3
4
5
6
7
8
9import pandas as pd df = pd.DataFrame({'a': np.random.randn(6), 'b': ['foo', 'bar'] * 3, 'c': np.random.randn(6)}) print(df) def add(a, b): return a + b df['Value'] = df.apply(lambda x: add(x['a'], x['c']), axis=1) print(df)
输出结果:
复制代码
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
49a b c 0 0.855374 foo 0.596161 1 0.176524 bar -1.598799 2 0.270245 foo -0.600968 3 0.958155 bar -0.097909 4 0.480277 foo -0.340813 5 -0.548556 bar 0.341068 a b c Value 0 0.855374 foo 0.596161 1.451535 1 0.176524 bar -1.598799 -1.422275 2 0.270245 foo -0.600968 -0.330723 3 0.958155 bar -0.097909 0.860246 4 0.480277 foo -0.340813 0.139464 5 -0.548556 bar 0.341068 -0.207488
最后
以上就是老实保温杯最近收集整理的关于pandas对DataFrame中多列数据的操作的全部内容,更多相关pandas对DataFrame中多列数据内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复