Reputation: 943
I have a problem with the output of sublime text. When I run this:
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['name'] = ['John', 'Steve', 'Sarah']
df.assign (age =[30,31,32])
print (df)
I get this result:
name
0 John
1 Steve
2 Sarah
I think this is a mistake, because I should can see the age column.
When I Run this code in jupyter I obtain:
name age
0 John 30
1 Steve 31
2 Sarah 32
Somebody have any idea what it happen?
thanks for the help
Upvotes: 3
Views: 265
Reputation: 13274
This is far from being a mistake or an interpreter mismatch.
When you call df.assign(age =[30,31,32])
, you are making a copy of df
with an additional column age
. When this is done in a REPL-like
editor, you see the ouput rendered. But in most text editors, this will not show because you are not printing the ouput. You are using the __repr__
method of the DataFrame
object. Unless the text editor has implemented a way to render __repr__
ouputs, you will never see the output of df.assign(age =[30,31,32])
.
Now, jupyter
, on the other hand, has a REPL
feature, and therefore will show commands based on the output's __repr__
method, without needing to call the print
function. As a result, jupyter
will show the output of df.assign(age =[30,31,32])
.
The bottom line remains that you are not changing the dataframe in place. Both sublime and jupyter
would print the results, if the code was written as follows:
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['name'] = ['John', 'Steve', 'Sarah']
df = df.assign(age=[30,31,32])
print(df)
I hope this helps.
Upvotes: 2
Reputation: 14121
The assign()
of dataframe returns a new object (a copy) with all the original columns in addition to the new ones - see pandas.DataFrame.assign.
So you have to assign it for getting its return value:
df = df.assign (age =[30,31,32])
Upvotes: 0