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
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