Python——使用带条件的loc进行dataframe修改操作

对dataframe中某一个字段进行批量修改操作。例如将data_df里hour字段内容为‘2014-10-21 00:00:00’的改为0,‘2014-10-21 01:00:00’的改为1,‘2014-10-21 02:00:00’的改为2。可使用带条件的loc进行操作。

a = data_df[data_df['hour']=='2014-10-21 00:00:00']['hour'][0]
b = data_df[data_df['hour']=='2014-10-21 01:00:00']['hour'][119006]
c = data_df[data_df['hour']=='2014-10-21 02:00:00']['hour'][256448]
 # 利用loc合并条件的方式来替换
data_df['hour'].loc[data_df['hour']==a] = 0
data_df['hour'].loc[data_df['hour']==b] = 1
data_df['hour'].loc[data_df['hour']==c] = 2

你可能感兴趣的:(Python技巧,CRUD)