LoginSignup
0
0

More than 3 years have passed since last update.

PandasのDataFrameの要素を列名指定で更新する

Last updated at Posted at 2020-02-08

目的

DataFrameの要素を列名指定で更新したい

環境

OS: ubuntu18.04
pythonバージョン: 3.6.9

ソースコード

main.py

#!/usr/bin/env python3
import pandas as pd

if __name__ == '__main__':
    dic = {'key1':[1,2,3],'key2':[4,5,6],'key3':[7,8,9]}

    df = pd.DataFrame.from_dict(dic)

    print(df)

    df.iloc[-1, df.columns.get_loc('key2')] = 10 #末尾行のKey2の値を更新

    print(df)

結果

   key1  key2  key3
0     1     4     7
1     2     5     8
2     3     6     9

   key1  key2  key3
0     1     4     7
1     2     5     8
2     3    10     9
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0