You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pandas如何选取DataFrame中最新非0值并转换为目标格式DataFrame

你现在已经通过dfs.last_valid_index()拿到了目标值所在的行索引r和月份列名c,直接用这几个变量构造目标DataFrame即可:

# 基于你已有的变量直接构造,不需要额外处理之前生成的x
import pandas as pd
result = pd.DataFrame(
    [[df.loc[r, 'Tahun'], c, dfs.loc[r, c]]],
    columns=['Tahun', 'Month', 'Value']
)

如果要从你已经得到的x Series转换,可以这么写:

result = pd.DataFrame(
    [[x.iloc[0], x.index[1], x.iloc[1]]],
    columns=['Tahun', 'Month', 'Value']
)

内容的提问来源于stack exchange,提问作者ohai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 03:00:00