[PYTHON] Update Pandas DataFrame elements by column name

Purpose

I want to update the elements of DataFrame by specifying the column name

environment

OS: ubuntu18.04 python version: 3.6.9

Source code

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 #Update the value of Key2 in the last line

    print(df)

result

   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

Recommended Posts

Update Pandas DataFrame elements by column name
Extract elements by Pandas column name x row number
update dataframe
Replace column names / values with pandas dataframe
How to get a specific column name and index name in pandas DataFrame
[Python] Sort the table by sort_values (pandas DataFrame)
Sort by pandas
Convert pandas dataframe elements to regular string type
Check if the expected column exists in Pandas DataFrame
3D plot Pandas DataFrame
Python application: Pandas # 3: Dataframe
A handy function to add a column anywhere in a Pandas DataFrame