0
$\begingroup$

I have a dataframe with 2 relevant columns.

+-------------+---------------+   
| Bezeichnung | Artikelgruppe |
+-------------+---------------+ 
|     A       |       1       |
|     B       |       2       |     
|     C       |       3       |
|     D       |       4       |
+-------------+---------------+

I want to paste the value of Bezeichnung into a new column (new_col) for all values of the column Artikelgruppe that are equal to 0.

$\endgroup$

1 Answer 1

0
$\begingroup$

This can be solved using a number of methods. One of the method is:

df['new_col']=df['Bezeichnung'][df['Artikelgruppe']==0]

This would result in a new column with the values of column Bezeichnung where values of column Artikelgruppe are 0 and the other values will be NaN. The NaN values could be easily replaced at any time of point.

+-------------+---------------+---------+   
| Bezeichnung | Artikelgruppe | new_col |
+-------------+---------------+---------+
|     A       |       1       |    NaN  |
|     B       |       2       |    NaN  |
|     C       |       0       |     C   |
|     D       |       4       |    Nan  |
+-------------+---------------+---------+
$\endgroup$
3
  • $\begingroup$ Thanks! Is this also possible with the lambda function? I tried to apply it but didn't work... $\endgroup$
    – Jonas Roth
    Jun 12, 2019 at 13:34
  • $\begingroup$ df['new_col'] = df['b'][df['a'].apply(lambda x: True if x==0 else False)]. This would solve the purpose $\endgroup$
    – thanatoz
    Jun 12, 2019 at 13:39
  • $\begingroup$ Thanks a lot!!!! $\endgroup$
    – Jonas Roth
    Jun 12, 2019 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.