Lets say there are two data frames: df1 contains 4 columns. The 'NAME' column contains the name of the cities (A, B, C). Each of the other columns represents a year (y0, y1, y2) containing the number of people living on that city.
np.random.seed(seed=34)
name = ['A','B','C']
y0 = np.random.random_integers(1,high=40, size=3)
y1 = np.random.random_integers(1,high=40, size=3)
y2 = np.random.random_integers(1,high=40, size=3)
df = pd.DataFrame(data={'NAME' : name, 'y0' : y0, 'y1' : y1, 'y2' : y2})
df
NAME y0 y1 y2
0 A 34 36 15
1 B 22 6 30
2 C 5 12 19
df2 contains 3 columns. The column 'NAME' contains the name of the cities. The 'y' column contains the value of the year (y0, y1, y2) and the 'i' column contains the number of people who have internet access.
y = ['y0', 'y1', 'y2', 'y0', 'y1', 'y2', 'y0', 'y1', 'y2']
name2 = ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C']
i = [15, 6, 12, 18, 4, 20, 3, 8, 2]
df2 = pd.DataFrame(data={'NAME':name2, 'y':y, 'i':i})
df2
NAME y i
0 A y0 15
1 A y1 6
2 A y2 23
3 B y0 18
4 B y1 17
5 B y2 20
6 C y0 3
7 C y1 24
8 C y2 2
I need to create a column on df2 that contains the values of df1 with the condition that the value of df2['NAME'] is equal to df1['NAME'] and df2['y'] is equal to the df1 column , in order to get the following result:
NAME y i v
0 A y0 15 34
1 A y1 6 36
2 A y2 12 15
3 B y0 18 22
4 B y1 4 6
5 B y2 20 30
6 C y0 3 5
7 C y1 8 12
8 C y2 2 19
The number of times that the cities names appear on df is not constant. Thank you in advance.
df2['y']suppose to equal?