I'd be grateful for any help anyone can offer on this, as have been tearing my hair out trying to solve it.
I have two python pandas dataframes, in simplified form they look like this:
df1
+-----+-----+-----+
| a_1 | a_2 | a_3 |
+-----+-----+-----+
| 0 | 2 | 5 |
| 1 | 3 | 4 |
| 0 | 0 | 0 |
+-----+-----+-----+
df2
+-----+-----+-----+
| b_1 | b_2 | b_3 |
+-----+-----+-----+
| 0 | 0 | 1 |
| 1 | 0 | 1 |
| 0 | 0 | 0 |
+-----+-----+-----+
I want to create a count column of non-null values (per row) in df1, if the equivalent cell is non-null in df2. The column titles in both dataframes are not the same, but are the same after the initial a_ and b_ prefixes.
So in this example the code would just count the third value in the first row, and the first and second in the second row. The new df1 dataframe would therefore look like this:
new_df1
+-----+-----+-----+----------------------+
| a_1 | a_2 | a_3 | count_if_nonnull_df2 |
+-----+-----+-----+----------------------+
| 0 | 2 | 5 | 1 |
| 1 | 3 | 4 | 2 |
| 0 | 0 | 0 | 0 |
+-----+-----+-----+----------------------+
Would anyone be able to help?! Thanks in advance.