Re: Pandas, create new column if previous column(s) are not in [None, '', np.nan]

2018-04-12 Thread zljubisic
On Wednesday, 11 April 2018 21:19:44 UTC+2, José María Mateos  wrote:
> On Wed, Apr 11, 2018, at 14:48, zlj...com wrote:
> > I have a dataframe:
> > [...]
> 
> This seems to work:
> 
> df1 = pd.DataFrame( { 'A' : ['a', 'b', '', None, np.nan],
>   'B'  : [None, np.nan, 'a', 
> 'b', '']})
> df1['C'] = df1[['A', 'B']].apply(lambda x: x[0] if x[1] in [None, '', np.nan] 
> else x[1], axis = 1)
> 
> Two notes:
> 
> - Do apply() on axis = 1, so you process every row.
> - You lambda function wasn't entirely correct, if I understood what you 
> wanted to do.
> 
> Cheers,
> 
> -- 
> José María (Chema) Mateos
> https://rinzewind.org/blog-es || https://rinzewind.org/blog-en

Thanks Jose, this what I needed. Thanks also to all others.
Regards.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pandas, create new column if previous column(s) are not in [None, '', np.nan]

2018-04-11 Thread Albert-Jan Roskam

On Apr 11, 2018 20:52, zljubi...@gmail.com wrote:
>
> I have a dataframe:
>
> import pandas as pd
> import numpy as np
>
> df = pd.DataFrame( { 'A' : ['a', 'b', '', None, np.nan],
>  'B'  : [None, np.nan, 'a', 'b', '']})
>
>   A B
> 0 a  None
> 1 b   NaN
> 2   a
> 3  None b
> 4   NaN
>
>
> I would like to create column C in the following way:
> column C = column B if column B is not in [None, '', np.nan]
> else column A
>
> How to do that?
>
> I tried:
>
> df['C'] = df[['A', 'B']].apply(lambda x: x[1] if x[1] in [None, '', np.nan] 
> else x[0])
>
> but I got all np.nan's.

This is another approach:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pandas, create new column if previous column(s) are not in [None, '', np.nan]

2018-04-11 Thread codewizard
On Wednesday, April 11, 2018 at 2:49:01 PM UTC-4, zlju...@gmail.com wrote:
> I have a dataframe:
> 
> import pandas as pd
> import numpy as np
> 
> df = pd.DataFrame( { 'A' : ['a', 'b', '', None, np.nan],
>  'B'  : [None, np.nan, 'a', 'b', '']})
> 
>   A B
> 0 a  None
> 1 b   NaN
> 2   a
> 3  None b
> 4   NaN  
> 
> 
> I would like to create column C in the following way:
> column C = column B if column B is not in [None, '', np.nan]
> else column A
> 
> How to do that?
> 
> I tried:
> 
> df['C'] = df[['A', 'B']].apply(lambda x: x[1] if x[1] in [None, '', np.nan] 
> else x[0])
> 
> but I got all np.nan's.
> 
> Where am I wrong?
> 
> I am expecting to get column C as ['a', 'b', 'a', 'b', NaN]
> 
> Regards.

Try this:

df['C'] = df['B'].where(df['B'], other=df['A'])

Regards,
Igor.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pandas, create new column if previous column(s) are not in [None, '', np.nan]

2018-04-11 Thread José María Mateos
On Wed, Apr 11, 2018, at 14:48, zljubi...@gmail.com wrote:
> I have a dataframe:
> [...]

This seems to work:

df1 = pd.DataFrame( { 'A' : ['a', 'b', '', None, np.nan],
  'B'  : [None, np.nan, 'a', 
'b', '']})
df1['C'] = df1[['A', 'B']].apply(lambda x: x[0] if x[1] in [None, '', np.nan] 
else x[1], axis = 1)

Two notes:

- Do apply() on axis = 1, so you process every row.
- You lambda function wasn't entirely correct, if I understood what you wanted 
to do.

Cheers,

-- 
José María (Chema) Mateos
https://rinzewind.org/blog-es || https://rinzewind.org/blog-en
-- 
https://mail.python.org/mailman/listinfo/python-list


Pandas, create new column if previous column(s) are not in [None, '', np.nan]

2018-04-11 Thread zljubisic
I have a dataframe:

import pandas as pd
import numpy as np

df = pd.DataFrame( { 'A' : ['a', 'b', '', None, np.nan],
 'B'  : [None, np.nan, 'a', 'b', '']})

  A B
0 a  None
1 b   NaN
2   a
3  None b
4   NaN  


I would like to create column C in the following way:
column C = column B if column B is not in [None, '', np.nan]
else column A

How to do that?

I tried:

df['C'] = df[['A', 'B']].apply(lambda x: x[1] if x[1] in [None, '', np.nan] 
else x[0])

but I got all np.nan's.

Where am I wrong?

I am expecting to get column C as ['a', 'b', 'a', 'b', NaN]

Regards.
-- 
https://mail.python.org/mailman/listinfo/python-list