Reputation: 11
So I am trying to write a function which edits a dataframe in place- drops a column and adds another column to it. Within the function I am getting the expected output, but when I am trying to print the dataframe outside the function, it is still printing the original values:
import pandas as pd
import numpy as np
def login_table(id_name_verified, id_password):
"""
:param id_name_verified: (DataFrame) DataFrame with columns: Id, Login, Verified.
:param id_password: (numpy.array) Two-dimensional NumPy array where each element
is an array that contains: Id and Password
:returns: (None) The function should modify id_name_verified DataFrame in-place.
It should not return anything.
"""
password= pd.DataFrame(data=id_password[:,1],columns=["Password"])
id_name_verified = id_name_verified.join(password["Password"])
id_name_verified.drop("Verified",axis=1,inplace=True)
return id_name_verified
id_name_verified = pd.DataFrame([[1, "JohnDoe", True], [2, "AnnFranklin", False]], columns=["Id", "Login", "Verified"])
global id_name_verified
id_password = np.array([[1, 987340123], [2, 187031122]], np.int32)
login_table(id_name_verified, id_password)
print(id_name_verified)
The last print statement is still printing:
Id Login Verified
0 1 JohnDoe True
1 2 AnnFranklin False
What am I missing? I am new to python. Please help!
Upvotes: 0
Views: 64
Reputation: 11
So instead of the join I had to use the loc function to add the password to the id_name_verified dataframe:
def login_table(id_name_verified, id_password):
password = pd.DataFrame(id_password, columns=["Id", "Password"])
id_name_verified.loc[:, 'Password'] = password['Password']
id_name_verified.drop("Verified",axis=1,inplace=True)
return None
This did the trick.
Upvotes: 1