Extracting specific selected columns to new DataFrame as a copy

前端 未结 7 1931
忘了有多久
忘了有多久 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: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
    

提交回复
热议问题