Derek Krantz
Derek Krantz

Reputation: 487

appending list to a dataframe

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

Upvotes: 2

Views: 140

Answers (1)

Vaishali
Vaishali

Reputation: 38415

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

Upvotes: 1

Related Questions