0

I have the foll dataframe (grp):

key  ColA   ColB
2    23.4   33.4
3    43.2   45.3
4    12.3   90.3
5    11.2   77.3

grp is the result of a groupby operation performed earlier. Here 'key' is the index.

How can I create another dataframe with 'key' and 'ColA' as its elements? Right now, I cannot seem to select 'key'

2
  • 1
    Is grp really a dataframe, or is it just a groupby object? Also, is key a column, or is it the index? Commented May 15, 2014 at 2:58
  • grp is a data frame, 'key' is index Commented May 15, 2014 at 3:00

1 Answer 1

1

You can use reset_index. Assuming grp is the DataFrame as you have described:

res = grp['ColA'].reset_index()
# res is now a DataFrame with key and ColA as the columns
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, does this extend if I want to select ColB as well? What will the syntax look like then?
You can either use grp[['ColA', 'ColB']].reset_index(), or simply grp.reset_index() if there are only two columns in total.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.