pandas loc函式 檢視與修改特定值
阿新 • • 發佈:2021-01-21
df.loc[index, column_name] 選取指定行和列的資料
目錄
栗子:
import numpy as np import pandas as pd df = pd.DataFrame(data = [['荔景','Kowloon'],['深井(荃灣)','New Territories'],['深井(屯門)','New Territories']],index = [1,2,3],columns = ['district','region'])#測試資料。
1.檢視同一行其他列的值
df.loc[df['district']=='荔景','region']
2.修改特定行中其他列的值
df.loc[df['district']=='荔景','region']='New Territories'
3.修改特定行的值
df.loc[df['district']=='深井(荃灣)','district']='深井'
Reference: