【发布时间】:2021-03-13 12:25:34
【问题描述】:
好的,所以我有两列“Price 1”和“Price 2”,“Price 1”中的一些值是 Nan,“Price 2”中的一些值是 Nan,但我想将它们连接起来。
我的代码和输出:
F3["Price 1"] = F3["Price 1"].fillna("0")
F3["Price 2"] = F3["Price 2"].fillna("0")
F3.index+=1
F3
Price 1 Price 2
1 0 54.95
2 34.95 0
3 0 99.99
4 34.84 0
5 124.95 0
6 0 101.50
所以我希望最终是下面的输出:
因此最终价格中的数据将从“价格2”填充,但只有0值将填充到“价格1”中
Final Price
1 54.95
2 34.95
3 99.99
4 34.84
5 124.95
6 101.50
【问题讨论】:
-
如果您的数据是相同的模式,那么您可以添加两个价格,即
F3['Final Price']=F3["Price 1"]+F3["Price 2"] -
df['Final Price'] = df.sum(axis=1)?? -
它只能 concat str 而不是 float 是我得到的错误
-
我假设你在做连接之前做了 fillna
-
是的,我已经完成了填充 na,因为它们之前是“Nan”值,我只是将它们设为 0。
标签: python python-3.x pandas dataframe python-requests