In Pandas, does .iloc method give a copy or view?

后端 未结 2 991
失恋的感觉
失恋的感觉 2020-12-05 10:25

I find the result is a little bit random. Sometimes it\'s a copy sometimes it\'s a view. For example:

df = pd.DataFrame([{\'name\':\'Marry\', \'age\':21},{\'         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 10:39

    You are starting with a DataFrame that has two columns with two different dtypes:

    df.dtypes
    Out: 
    age      int64
    name    object
    dtype: object
    

    Since different dtypes are stored in different numpy arrays under the hood, you have two different blocks for them:

    df.blocks
    
    Out: 
    {'int64':           age
     student1   21
     student2   24, 'object':            name
     student1  Marry
     student2   John}
    

    If you attempt to slice the first row of this DataFrame, it has to get one value from each different block which makes it necessary to create a copy.

    df2.is_copy
    Out[40]: 
    

    In the second attempt, you are changing the dtypes. Since 'old' cannot be stored in an integer array, it casts the Series as an object Series.

    df.loc['student3'] = ['old','Tom']
    
    df.dtypes
    Out: 
    age     object
    name    object
    dtype: object
    

    Now all data for this DataFrame is stored in a single block (and in a single numpy array):

    df.blocks
    
    Out: 
    {'object':           age   name
     student1   21  Marry
     student2   24   John
     student3  old    Tom}
    

    At this step, slicing the first row can be done on the numpy array without creating a copy, so it returns a view.

    df3._is_view
    Out: True
    

提交回复
热议问题