'Is there a way to append values of a column in a dataframe based on a condition?
I have two datasets, df1 and df2 and would like to create a third column in df2 by appending values in type column of df1 to their corresponding ids in df2. Please note that the ids in df2 can have duplicates and should not be deleted. The dataframes and expected output is as follows:
data1 = [[1283, 234, 8], [1313, 155, 4],
[1837, 987,6], [1443, 200, 0],
[1923, 224, 1], [1912, 247, 7],
[1176, 228, 2], [1865, 248, 6],
[1219, 265, 3], [1255, 862, 1]]
df1 = pd.DataFrame(data1, columns =['id', 'type', 'qty'])
print(df1)
id type qty
0 1283 234 8
1 1313 155 4
2 1837 987 6
3 1443 200 0
4 1923 224 1
5 1912 247 7
6 1176 228 2
7 1865 248 6
8 1219 265 3
9 1255 862 1
I have another dataframe as follows:
data2 =[[1313, 0], [1313,0],
[1443, 0], [1176,0],
[1912,1], [1912,1],
[1912, 1], [1283, 0],
[1837, 1], [1837, 1],
[1837, 1], [1923, 0],
[1865, 0], [1865, 0],
[1219, 1], [1255,1]]
df2 = pd.DataFrame(data2, columns =['id', 'class'])
print(df2)
id _class
0 1313 0
1 1313 0
2 1443 0
3 1176 0
4 1912 1
5 1912 1
6 1912 1
7 1283 0
8 1837 1
9 1837 1
10 1837 1
11 1923 0
12 1865 0
13 1865 0
14 1219 1
15 1255 1
I would like to append the 'type' value in df1 to their corresponding id to get the following:
id _class type
0 1313 0 155
1 1313 0 155
2 1443 0 200
3 1176 0 228
4 1912 1 247
5 1912 1 247
6 1912 1 247
7 1283 0 234
8 1837 1 987
9 1837 1 987
10 1837 1 987
11 1923 0 224
12 1865 0 248
13 1865 0 248
14 1219 1 265
15 1255 1 862
Solution 1:[1]
df2.merge(df1, on="id").drop(["qty"],axis=1)
id class type
0 1313 0 155
1 1313 0 155
2 1443 0 200
3 1176 0 228
4 1912 1 247
5 1912 1 247
6 1912 1 247
7 1283 0 234
8 1837 1 987
9 1837 1 987
10 1837 1 987
11 1923 0 224
12 1865 0 248
13 1865 0 248
14 1219 1 265
15 1255 1 862
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Igor Goryachev |
