1

I have an empty dataframe (data) that I definied as follows:

 data=pd.DataFrame({'A':[],'B':[],'C':[],'D':[]})

and by means of a for loop I am obtaining several temporary dataframes that look like this:

index   order_count
 C             3
 A             1
 B             1

I would like to populate "data" by using the data these temporary dataframes. I guess I would have to check for the "order_count" column into the "index" and add the corresponding "order_count". After that, perhaps concatenate the temporary results.

I would like to obtain the following output for "data":

time_interval   A  B  C  D
     1          1  1  3  0
     2          1  1  1  1
    ...
3
  • 2
    Please show your expected output, don't just describe it. Commented Jun 6, 2019 at 20:55
  • Yes, I have provided more details about the type of output. Commented Jun 7, 2019 at 15:14
  • Thanks, that was helpful. I've written an answer below. Commented Jun 7, 2019 at 15:19

2 Answers 2

1

One option is using append which returns a copy (relatively inefficient when calling append multiple times):

data

Empty DataFrame
Columns: [A, B, C, D]
Index: []

temp_df

  index  order_count
0     C            3
1     A            1
2     B            1

data.append(temp_df.set_index('index')['order_count'], ignore_index=True)

     A    B    C   D
0  1.0  1.0  3.0 NaN

Another option is in-place assignment with loc:

data.loc[len(df),:] = temp_df.set_index('index')['order_count']
data

     A    B    C   D
4  1.0  1.0  3.0 NaN
Sign up to request clarification or add additional context in comments.

Comments

0

If I've well understood your problem, I guess this could do the trick :

#df being the dataFrame and temp being the one you are wellling to add
df = df.append(temp.set_index('index').T, ignore_index = True)

Comments

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.