I have a data frame of this structure which I would like to populate:
V1 V2 V3 V4 V5 V6 V7 V8
1 ID_CODE 0 0 0 0 0 0 0
2 THIS 0 0 0 0 0 0 0
3 ISAROW 0 0 0 0 0 0 0
4 01 0 0 0 0 0 0 0
5 02 0 0 0 0 0 0 0
6 03 0 0 0 0 0 0 0
7 ID_CODE 0 0 0 0 0 0 0
8 THESE 0 0 0 0 0 0 0
9 ARE 0 0 0 0 0 0 0
10 MORE 0 0 0 0 0 0 0
11 ROWS 0 0 0 0 0 0 0
12 01 0 0 0 0 0 0 0
13 02 0 0 0 0 0 0 0
14 03 0 0 0 0 0 0 0
15 ROW 0 0 0 0 0 0 0
And this data frame with the numbers to populate it:
V2_1 V2_2 V2_3 V2_4 V2_5 V2_6 V2_7
1 786 786 786 786 786 786 786
2 786 786 786 786 786 786 786
3 78 78 78 78 78 78 78
4 78 78 78 78 78 78 78
5 78 78 78 78 78 78 78
6 78 78 78 78 78 78 78
These numbers are to go into columns V2:V8 and only in rows in which V1 is a number. The rows in which V1 is a string are to stay as zeroes.
(1) IF condition – Set of numbers Suppose that you created a DataFrame in Python that has 10 numbers (from 1 to 10). You then want to apply the following IF conditions: If the number is equal or lower than 4, then assign the value of 'True' Otherwise, if the number is greater than 4, then assign the value of 'False'
You can create a conditional column in pandas DataFrame by using np. where() , np. select() , DataFrame. map() , DataFrame.
We can use the assign() method to fill the columns with a single value. Generally, the assign() method is used to add a new column to an existing DataFrame.
If df1
is the original and df2
the replacement, then we can use Map
to replace the subset.
## find the rows with only digits in the first column
rows <- grepl("^\\d+$", df1$V1)
## replace the subset with 'df2'
df1[rows, -1] <- Map("[<-", df1[rows, -1], df2)
df1
# V1 V2 V3 V4 V5 V6 V7 V8
# 1 ID_CODE 0 0 0 0 0 0 0
# 2 THIS 0 0 0 0 0 0 0
# 3 ISAROW 0 0 0 0 0 0 0
# 4 01 786 786 786 786 786 786 786
# 5 02 786 786 786 786 786 786 786
# 6 03 78 78 78 78 78 78 78
# 7 ID_CODE 0 0 0 0 0 0 0
# 8 THESE 0 0 0 0 0 0 0
# 9 ARE 0 0 0 0 0 0 0
# 10 MORE 0 0 0 0 0 0 0
# 11 ROWS 0 0 0 0 0 0 0
# 12 01 78 78 78 78 78 78 78
# 13 02 78 78 78 78 78 78 78
# 14 03 78 78 78 78 78 78 78
# 15 ROW 0 0 0 0 0 0 0
Or, another way would be to use replace()
df1[rows, -1] <- Map(function(x, y) replace(x, rows, y), df1[-1], df2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With