向现有行添加值-数据帧

5

我正在将一些(来自于 json - 字典的)天气数据追加到 DataFrame 中。

我希望得到下面这样的结果:

        天気             
 0  状態: Clouds    風速: 2.1m
 1     NaN          向き: 230

但是我有这个。
        天気             
 0  状態: Clouds        NaN
 1      NaN          風速: 2.1m
 2      NaN           向き: 230

我该如何更改代码才能实现这样的效果?以下是代码:
df = pd.DataFrame(columns=['天気','風'])
df = df.append({'天気': weather_status}, ignore_index=True) # 状態: Clouds - value
df = df.append({'風': wind_speed}, ignore_index=True) # 風速: 2.1m -value
df = df.append({'風': wind_deg}, ignore_index=True) # 向き: 230 -value
print(df)
1个回答

2

一种方法可能是:

df = pd.DataFrame(columns=['天気','風'])
df = df.append({'天気': weather_status, '風': wind_speed}, ignore_index=True)
df = df.append({'風': wind_deg}, ignore_index=True)
print(df)

最初的回答

输出不包括索引

print(df.to_string(index=False))

有没有办法去掉这些索引号? - user11040244

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接