Manan Sheth
Manan Sheth

Reputation: 316

Problem while adding new column in Panda dataframe using Python

I have tried various methods to add new column to a Panda dataframe but I get the same result. Methods tried:

call_duration is a list having same number of items as in the data frame.

df['Duration_sec'] = pd.Series(call_duration,index=np.arange(len(df)))

and

df['Duration_sec'] = pd.Series(call_duration,index=df.index)

and

# df['Duration_sec'] = np.array(call_duration)

All three gave the same result as under-enter image description here

I don't understand why the new column is added to new line? And why is there a \ at the end of the first line?

Upvotes: 0

Views: 47

Answers (2)

aprilangel
aprilangel

Reputation: 379

You can just do

df['Duration_sec'] = call_duration

"\" means the dataframe is wider than your screen and will continue.

Upvotes: 1

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

"The new column is not added to a new line"

The DataFrame is wider than the screen and hence continued in next row. In python the \ is usually used to denote join

To add a column, Simply use df.assign

df.assign(Duration_sec=call_duration)

Upvotes: 2

Related Questions