Learning Python here, and any help on this is much appreciated. I have a two-part problem, and although I have created a solution to the first part, there has to be a much more pythonic way to accomplish the goal. The second part, not so sure how to proceed.
I have columns of unique IDs in two separate dataframes. I want to count the number of times that a uid in df_2 uid column occurs in df_1's uid column, and then add that uid to a list if it is both. The following code example works for me, but i am worried there is a problem somewhere, and there has to be much better way.
data = {'uid':['uid1', 'uid2', 'uid3', 'uid4'], 'value': [1, 2, 3, 4]}
df = pd.DataFrame(data)
data1 = {'uid':['uid4', 'uid2', 'uid5'], 'value1': ["", 5, 6]}
df1 = pd.DataFrame(data1)
count_val_in_both_df = 0
list_val_in_both_df = []
for x in range(len(df1.iloc[:, 0])) :
if df1.iloc[x, 0] in df.iloc[:, 0].values :
count_val_in_both_df += 1
list_val_in_both_df.append(df1.iloc[x, 0])
print('count = ' + str(count_val_in_both_df))
print(list_val_in_both_df)
Which outputs:
df
uid value
0 uid1 1
1 uid2 2
2 uid3 3
3 uid4 4
df1
uid value1
0 uid4
1 uid2 5
2 uid5 6
count = 2
['uid4', 'uid2']
The second part is creating a column in df for a values in df1 and adding the value from df1. I am pretty lost on this part of it, but want an outcome like this:
{ uid value value1
0 uid1 1
1 uid2 2 5
2 uid3 3
3 uid4 4 }