2

I'm not understanding the behavior of appending data to a dataframe.

what i want to do is append a list with two values like this:

temp_arr
[Timestamp('2018-01-01 00:00:00', freq='D'), 0]

to a dataframe with two columns. However, its adding two rows, not one row with two columns:

df_final = df_final.append(pd.DataFrame(temp_arr),ignore_index=True)

produces something like this:

df_final.head()
0
0   2018-01-01 00:00:00
1   0
2   2018-01-02 00:00:00
3   0
4   2018-01-03 00:00:00

instead of something like this

df_final.head()

col1  col2
0     2018-01-01 00:00:00
2
  • Try, df_final = pd.concat([df_final, pd.DataFrame(temp_arr)], ignore_index=True) Commented Mar 19, 2018 at 16:40
  • @coldspeed nope, got the same results - does it have something to do with the fact I'm using a timestamp? Commented Mar 19, 2018 at 16:46

1 Answer 1

1

Try

df = pd.DataFrame(columns = ['col1', 'col2'])
temp_arr = [pd.Timestamp('2018-01-01 00:00:00', freq='D'), 0]
df.append(pd.Series(temp_arr, index = ['col2', 'col1']),ignore_index=True)

You get

    col1    col2
0   0   2018-01-01
Sign up to request clarification or add additional context in comments.

4 Comments

this doesn't work. running your code for me produces an empty dataframe. Also, pd.Series produces one column - I want to essentially two columns to a dataframe
Can you share more details, how are you creating the empty data frame? Because it works for me
my error it works now. Can you explain the behavior of the Series in your code? i thought index sets an index for rows not columns as in this case
When you append a data frame to another data frame, it maps the column names. Whereas when you append a series to the dataframe, it maps the indices of the series with that of columns of the dataframe

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.