Python 要在条件下联接两个数据帧的最后一行吗
我有两个数据框quantity和price,我想将quantity数据框的最后一行连接到price,其中Python 要在条件下联接两个数据帧的最后一行吗,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据框quantity和price,我想将quantity数据框的最后一行连接到price,其中c不是nan 我编写了这些查询,但没有得到所需的输出: price=pd.concat(价格、数量[“a”、“b”、“c”].tail(1).isnotnull()) 我想要的是: quantity: a b c 3 1 nan 3 2 8 7 5 9 4 8 nan price 34 我相信您需要删除缺少的值,对于最后一行,为一
c
不是nan
我编写了这些查询,但没有得到所需的输出:
price=pd.concat(价格、数量[“a”、“b”、“c”].tail(1).isnotnull())
我想要的是:
quantity:
a b c
3 1 nan
3 2 8
7 5 9
4 8 nan
price
34
我相信您需要删除缺少的值,对于最后一行,为一行数据帧添加了双
[]
:
price a b c
34 7 5 9
详细信息:
df=pd.concat([price.reset_index(drop=True),
quantity[["a","b","c"]].dropna(subset=['c']).iloc[[-1]].reset_index(drop=True)],
axis=1)
print (df)
price a b c
0 34 7 5 9.0
我会在
notnull
上过滤df
,然后简单地将价格添加到其中:
print (quantity[["a","b","c"]].dropna().iloc[[-1]])
a b c
2 7 5 9.0
其中c是您的列名
new_df = df[df['c'].notnull()]
如果您的dfs是:
new_df['price'] = 32 # or the price from your df
您可以这样做:
df = pd.DataFrame([[3,1,np.nan], [3,2,8], [7,5,9], [4,8,np.nan]], columns=['a','b','c'])
df2 = pd.DataFrame([34], columns=['price'])
输出:
final_df = pd.concat([df.dropna(subset=['c']).tail(1).reset_index(drop=True), df2], axis=1)
@耶斯雷尔能帮我吗??
a b c price
0 7 5 9.0 34