Python——pandas條件賦值

方法一:

df。loc[df[‘設定條件的列名’] == ‘’, ‘要的修改列名’] = 0

方法二:

df[‘new’]=np。where(df[‘list’]==‘’,0,df[‘amount’])

方法三:(不建議,會有warning)

df。B[df。A>4] = 0

E:\Anaconda\lib\site-packages\ipykernel_launcher。py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrameSee the caveats in the documentation: https://pandas。pydata。org/pandas-docs/stable/user_guide/indexing。html#returning-a-view-versus-a-copy “”“Entry point for launching an IPython kernel。E:\Anaconda\lib\site-packages\pandas\core\generic。py:8765: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrameSee the caveats in the documentation: https://pandas。pydata。org/pandas-docs/stable/user_guide/indexing。html#returning-a-view-versus-a-copy self。_update_inplace(new_data)