Reputation: 631
I tried to modify the dataframe through function and return the modified dataframe. Somehow, it is not reflected. In the below code, I pass a dataframe 'ding' to function 'test' and create a new column 'C' and return the modified dataframe. I expected the test_ding df to have 3 columns but could see only two columns. Any help is highly appreciated.
s1 = pd.Series([1,3,5,6,8,10,1,1,1,1,1,1])
s2 = pd.Series([4,5,6,8,10,1,7,1,6,5,4,3])
ding=pd.DataFrame({'A':s1,'B':s2})
def test(ding):
ding.C=ding.A+ding.B
return ding
test_ding=test(ding)
Upvotes: 1
Views: 2372
Reputation: 153460
Let's try this:
s1 = pd.Series([1,3,5,6,8,10,1,1,1,1,1,1])
s2 = pd.Series([4,5,6,8,10,1,7,1,6,5,4,3])
ding=pd.DataFrame({'A':s1,'B':s2})
def test(ding):
ding = ding.assign(C=ding.A+ding.B)
return ding
test_ding=test(ding)
print(test_ding)
Output:
A B C
0 1 4 5
1 3 5 8
2 5 6 11
3 6 8 14
4 8 10 18
5 10 1 11
6 1 7 8
7 1 1 2
8 1 6 7
9 1 5 6
10 1 4 5
11 1 3 4
Upvotes: 1
Reputation: 7806
ding.C doesn't exist and can't be accessed like an attribute. You need to change the line to
ding['C']=ding.A+ding.B
You can create an column like ding['A'] = blah
which then turns into an attribute of the DataFrame. But you can't go the other way around.
Upvotes: 5