1

I have a dataframe with multiple columns (a, b, c, d). I grouped my dataframe by columns 'c' and 'd' and integrated column 'a' with respect to column 'b'. This gave me the output in the following format (the values which go like '1500, 1400 ... 700' are the result of the performed integration):

c d
1 10  1500
  20  1400
  30  1300
2 10  1200
  20  1100
  30  1000
3 10   900
  20   800
  30   700

I was wondering how can I convert that output into a dataframe, where it would show columns 'c', 'd', and also create a new column which would contain the result of integration?

0

2 Answers 2

2

Please try:

df = df.reset_index()
Sign up to request clarification or add additional context in comments.

2 Comments

the output itself that I get is not in a 'DataFrame' format
If that is groupby object try .first()
1

I recently created a DataFrame from similar data.

First, create simple lists for each c-value of the complex list.

c1_vals = [1500, 1400, 1300], etc

Create a DataFrame with a dictionary with c-values as keys and lists as values.

ex_df = pd.DataFrame({'1': c1_vals, '2': c2_vals, '3': c3_vals})

The d-values from the complex-data correspond to each lists' index position.

ex_df['1'][0] represents c:1 d:10

You could create variables for individual d-values like this.

c1d10 = ex_df['1'][0]

3 Comments

My answer will be helpful if your issue is that Pandas DataFrame doesn't accept the type of data your attempting to input @Rauan_Saturin
Mmm, I see the logic, it makes sense. But in my case I have more than a hundred thousand rows, I can't really think of a way to work around your method without manually typing in all of the data. Any ideas?
Depending on how your data is structured, you could potentially use for-loops to append the values to lists. i.e. for val in c1: c1_val.append(val)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.