首页 > 解决方案 > 向现有行添加值 -DataFrame

问题描述

我正在将一些天气数据(来自 json-dict) - 用日语附加到 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)

标签: pythonpython-3.xpandasdataframe

解决方案


一种方法可能是:

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))

推荐阅读