我有一个通过xlwings检索的数据框。我想在A.OUT-B.IN和A.OUT-C.IN两列中产生两个结果,但始终显示NaN。有一组三个相同的MO。每个MO都有三个不同的挡块A,B,C。所有挡块都有输入量和输出量。要计算A.OUT-B.IN,需要使用A停止输出量减去B停止输入量的相同MO。 A.OUT-C.IN的差异是输入量为C档。如果获得NaN值该怎么办?我尝试将两个系列更改为数字,搜索google,然后阅读熊猫文档仍然找不到解决方案。
这是示例代码:
import pandas as pd
df = pd.DataFrame({'MO': ['510-20200701001', '510-20200701001', '510-20200701001', '510-20200701002', '510-20200701002', '510-20200701002', '510-20200701003', '510-20200701003', '510-20200701003', '510-20200701004', '510-20200701004', '510-20200701004', '510-20200701005', '510-20200701005', '510-20200701005', '510-20200701006', '510-20200701006', '510-20200701006'],
'Stop Name': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'],
'Amount of Input': [21000, 22112, 22476, 12000, 12609, 12775, 15000, 15595, 15844, 600, 775, 790, 1000, 1149, 1176, 6000, 6225, 6289],
'Amount of Output': [22400, 22057, 22330, 12800, 12586, 12685, 16000, 15587, 15718, 800, 775, 783, 1200, 1139, 1162, 6400 ,6225, 6278],
'A.OUT-B.IN':['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], 'A.OUT-C.IN': ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] })
df.loc[df['Stop Name'] == 'B', 'A.OUT-B.IN'] = df.loc[df['Stop Name'] == 'A', 'Amount of Output'] - df.loc[df['Stop Name'] == 'B', 'Amount of Input']
df.loc[df['Stop Name'] == 'C', 'A.OUT-C.IN'] = df.loc[df['Stop Name'] == 'A', 'Amount of Output'] - df.loc[df['Stop Name'] == 'C', 'Amount of Input']
这是输出
MO Stop Name Amount of Input Amount of Output A.OUT-B.IN A.OUT-C.IN
0 510-20200701001 A 21000 22400
1 510-20200701001 B 22112 22057 NaN
2 510-20200701001 C 22476 22330 NaN
3 510-20200701002 A 12000 12800
4 510-20200701002 B 12609 12586 NaN
5 510-20200701002 C 12775 12685 NaN
6 510-20200701003 A 15000 16000
7 510-20200701003 B 15595 15587 NaN
8 510-20200701003 C 15844 15718 NaN
9 510-20200701004 A 600 800
10 510-20200701004 B 775 775 NaN
11 510-20200701004 C 790 783 NaN
12 510-20200701005 A 1000 1200
13 510-20200701005 B 1149 1139 NaN
14 510-20200701005 C 1176 1162 NaN
15 510-20200701006 A 6000 6400
16 510-20200701006 B 6225 6225 NaN
17 510-20200701006 C 6289 6278 NaN
这里是输出内容
MO Stop Name Amount of Input Amount of Output A.OUT-B.IN A.OUT-C.IN
0 510-20200701001 A 21000 22400
1 510-20200701001 B 22112 22057 288
2 510-20200701001 C 22476 22330 -76
3 510-20200701002 A 12000 12800
4 510-20200701002 B 12609 12586 191
5 510-20200701002 C 12775 12685 25
6 510-20200701003 A 15000 16000
7 510-20200701003 B 15595 15587 405
8 510-20200701003 C 15844 15718 156
9 510-20200701004 A 600 800
10 510-20200701004 B 775 775 25
11 510-20200701004 C 790 783 10
12 510-20200701005 A 1000 1200
13 510-20200701005 B 1149 1139 51
14 510-20200701005 C 1176 1162 24
15 510-20200701006 A 6000 6400
16 510-20200701006 B 6225 6225 175
17 510-20200701006 C 6289 6278 111
答案 0 :(得分:1)
如果知道所有相应向量的长度都是正确的,那么您可以做什么,只需在调用中添加“ .values”,如下所示。
import pandas as pd
df = pd.DataFrame({'MO': ['510-20200701001', '510-20200701001', '510-20200701001', '510-20200701002', '510-20200701002', '510-20200701002', '510-20200701003', '510-20200701003', '510-20200701003', '510-20200701004', '510-20200701004', '510-20200701004', '510-20200701005', '510-20200701005', '510-20200701005', '510-20200701006', '510-20200701006', '510-20200701006'],
'Stop Name': ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'],
'Amount of Input': [21000, 22112, 22476, 12000, 12609, 12775, 15000, 15595, 15844, 600, 775, 790, 1000, 1149, 1176, 6000, 6225, 6289],
'Amount of Output': [22400, 22057, 22330, 12800, 12586, 12685, 16000, 15587, 15718, 800, 775, 783, 1200, 1139, 1162, 6400 ,6225, 6278],
'A.OUT-B.IN':['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], 'A.OUT-C.IN': ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] })
df.loc[df['Stop Name'] == 'B', 'A.OUT-B.IN'] = df.loc[df['Stop Name'] == 'A', 'Amount of Output'].values - df.loc[df['Stop Name'] == 'B', 'Amount of Input'].values
df.loc[df['Stop Name'] == 'C', 'A.OUT-C.IN'] = df.loc[df['Stop Name'] == 'A', 'Amount of Output'].values - df.loc[df['Stop Name'] == 'C', 'Amount of Input'].values