Selecting multiple columns in a pandas dataframe

后端 未结 19 1824
醉话见心
醉话见心 2020-11-22 00:08

I have data in different columns but I don\'t know how to extract it to save it in another variable.

index  a   b   c
1      2   3   4
2      3   4   5
         


        
19条回答
  •  青春惊慌失措
    2020-11-22 00:20

    Starting with 0.21.0, using .loc or [] with a list with one or more missing labels is deprecated in favor of .reindex. So, the answer to your question is:

    df1 = df.reindex(columns=['b','c'])

    In prior versions, using .loc[list-of-labels] would work as long as at least 1 of the keys was found (otherwise it would raise a KeyError). This behavior is deprecated and now shows a warning message. The recommended alternative is to use .reindex().

    Read more at Indexing and Selecting Data

提交回复
热议问题