Python 3.x 如果另一列';s值在熊猫中为NaN

Python 3.x 如果另一列';s值在熊猫中为NaN,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,如果volume的行值为NaN,我想将price,quantity列移到右侧的一个位置: date price quantity volume 0 2015/01/28 10:00:00 0 5820 NaN 1 2020/02/27 11:10:44 3,164,886.76 0 8338.0 2 2018/07/19 10:27:32 16,220,0

如果
volume
的行值为
NaN
,我想将
price
quantity
列移到右侧的一个位置:

                  date          price  quantity   volume
0  2015/01/28 10:00:00              0      5820      NaN
1  2020/02/27 11:10:44  3,164,886.76          0   8338.0
2  2018/07/19 10:27:32    16,220,000          0   9229.0
3  2019/11/02 10:29:49     2,847,767          0   8321.0
4  2018/03/18 10:00:00     1,194,656          0   9824.0
5  2018/11/17 10:00:00              0      5108      NaN
6  2017/12/26 10:20:44     2,909,552          0  11153.0
7  2019/02/17 10:47:35              0      7341      NaN
8  2019/09/26 10:11:07     3,539,472          0   8086.0
9  2017/04/10 10:00:00     3,066,040          0  10682.0
我已经通过
df[df['volume'].isnull()]

只是想知道我怎么能得到这样的输出,谢谢

                  date          price  quantity  volume
0  2015/01/28 10:00:00            NaN         0    5820
1  2020/02/27 11:10:44  3,164,886.76          0    8338
2  2018/07/19 10:27:32    16,220,000          0    9229
3  2019/11/02 10:29:49     2,847,767          0    8321
4  2018/03/18 10:00:00     1,194,656          0    9824
5  2018/11/17 10:00:00            NaN         0    5108
6  2017/12/26 10:20:44     2,909,552          0   11153
7  2019/02/17 10:47:35            NaN         0    7341
8  2019/09/26 10:11:07     3,539,472          0    8086
9  2017/04/10 10:00:00     3,066,040          0   10682

使用
ffill

s = df["volume"].isnull()
df.loc[s] = df.loc[s].ffill(1)
df.loc[s,"quantity"] = 0 #or df.loc[s,"price"] if you want to move price
print (df)

                  date         price quantity volume
0  2015/01/28 10:00:00             0        0   5820
1  2020/02/27 11:10:44  3,164,886.76        0   8338
2  2018/07/19 10:27:32    16,220,000        0   9229
3  2019/11/02 10:29:49     2,847,767        0   8321
4  2018/03/18 10:00:00     1,194,656        0   9824
5  2018/11/17 10:00:00             0        0   5108
6  2017/12/26 10:20:44     2,909,552        0  11153
7  2019/02/17 10:47:35             0        0   7341
8  2019/09/26 10:11:07     3,539,472        0   8086
9  2017/04/10 10:00:00     3,066,040        0  10682

谢谢,我需要一列一列地移动,对吗?我相信是这样@ahbon。
price
quantity
需要右移,而不是
price
quantity
name
address
,如果
volume
的行值是
NaN
,则需要移动,我应该在您的代码中修改什么?您可以继续添加
df.loc[s,“address”]=df.loc[s,“name”]
。。。我不熟悉使用
loc
一次完成所有操作的方法。对于移动多个列,继续为每个移动的列向代码中添加
.loc
assignment是没有意义的。您应该改用
shift
s = df["volume"].isnull()
df.loc[s] = df.loc[s].ffill(1)
df.loc[s,"quantity"] = 0 #or df.loc[s,"price"] if you want to move price
print (df)

                  date         price quantity volume
0  2015/01/28 10:00:00             0        0   5820
1  2020/02/27 11:10:44  3,164,886.76        0   8338
2  2018/07/19 10:27:32    16,220,000        0   9229
3  2019/11/02 10:29:49     2,847,767        0   8321
4  2018/03/18 10:00:00     1,194,656        0   9824
5  2018/11/17 10:00:00             0        0   5108
6  2017/12/26 10:20:44     2,909,552        0  11153
7  2019/02/17 10:47:35             0        0   7341
8  2019/09/26 10:11:07     3,539,472        0   8086
9  2017/04/10 10:00:00     3,066,040        0  10682