revoltman
revoltman

Reputation: 131

What is wrong with this pandas code?

import pandas as pd
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
s1.append(s2)
print(s1)

Such a simple thing but Its not appending. Out up is : 0 1 1 2 2 3 dtype: int64 It just prints s1. Its not appending? What silly mistake am I doing here?

Upvotes: 1

Views: 59

Answers (3)

Kewl
Kewl

Reputation: 3417

The append function returns a new object, instead of modifying the object calling it. Most pandas functions are not 'in place' by default, meaning they return a new object (some allow you to specify to do the operation in place, but append is not one of them).

Instead, you can just reassign s1:

import pandas as pd
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
s1 = s1.append(s2)
print(s1)

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153460

import pandas as pd
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
s1 = s1.append(s2)
print(s1)

Upvotes: 2

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95908

Because .append returns a new series, it doesn't mutate in place (like list.append). Try:

import pandas as pd
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
s3 = s1.append(s2)
print(s3)

Upvotes: 2

Related Questions