Set value to an entire column of a pandas dataframe

前端 未结 8 2063
甜味超标
甜味超标 2020-12-13 05:54

I\'m trying to set the entire column of a dataframe to a specific value.

In  [1]: df
Out [1]: 
     issueid   industry
0        001        xxx
1        002           


        
8条回答
  •  生来不讨喜
    2020-12-13 06:36

    Python can do unexpected things when new objects are defined from existing ones. You stated in a comment above that your dataframe is defined along the lines of df = df_all.loc[df_all['issueid']==specific_id,:]. In this case, df is really just a stand-in for the rows stored in the df_all object: a new object is NOT created in memory.

    To avoid these issues altogether, I often have to remind myself to use the copy module, which explicitly forces objects to be copied in memory so that methods called on the new objects are not applied to the source object. I had the same problem as you, and avoided it using the deepcopy function.

    In your case, this should get rid of the warning message:

    from copy import deepcopy
    df = deepcopy(df_all.loc[df_all['issueid']==specific_id,:])
    df['industry'] = 'yyy'
    

    EDIT: Also see David M.'s excellent comment below!

    df = df_all.loc[df_all['issueid']==specific_id,:].copy()
    df['industry'] = 'yyy'
    

提交回复
热议问题