Extracting specific selected columns to new DataFrame as a copy

前端 未结 7 1910
忘了有多久
忘了有多久 2020-12-04 04:59

I have a pandas DataFrame with 4 columns and I want to create a new DataFrame that only has three of the columns. This question is similar

相关标签:
7条回答
  • 2020-12-04 05:15

    As far as I can tell, you don't necessarily need to specify the axis when using the filter function.

    new = old.filter(['A','B','D'])
    

    returns the same dataframe as

    new = old.filter(['A','B','D'], axis=1)
    
    0 讨论(0)
  • 2020-12-04 05:20

    There is a way of doing this and it actually looks similar to R

    new = old[['A', 'C', 'D']].copy()
    

    Here you are just selecting the columns you want from the original data frame and creating a variable for those. If you want to modify the new dataframe at all you'll probably want to use .copy() to avoid a SettingWithCopyWarning.

    An alternative method is to use filter which will create a copy by default:

    new = old.filter(['A','B','D'], axis=1)
    

    Finally, depending on the number of columns in your original dataframe, it might be more succinct to express this using a drop (this will also create a copy by default):

    new = old.drop('B', axis=1)
    
    0 讨论(0)
  • 2020-12-04 05:24

    The easiest way is

    new = old[['A','C','D']]
    

    .

    0 讨论(0)
  • 2020-12-04 05:24

    Another simpler way seems to be:

    new = pd.DataFrame([old.A, old.B, old.C]).transpose()
    

    where old.column_name will give you a series. Make a list of all the column-series you want to retain and pass it to the DataFrame constructor. We need to do a transpose to adjust the shape.

    In [14]:pd.DataFrame([old.A, old.B, old.C]).transpose()
    Out[14]: 
       A   B    C
    0  4  10  100
    1  5  20   50
    
    0 讨论(0)
  • 2020-12-04 05:25

    Generic functional form

    def select_columns(data_frame, column_names):
        new_frame = data_frame.loc[:, column_names]
        return new_frame
    

    Specific for your problem above

    selected_columns = ['A', 'C', 'D']
    new = select_columns(old, selected_columns)
    
    0 讨论(0)
  • 2020-12-04 05:32

    columns by index:

    # selected column index: 1, 6, 7
    new = old.iloc[: , [1, 6, 7]].copy() 
    
    0 讨论(0)
提交回复
热议问题