简体   繁体   中英

Pandas create a new column based on non-null value of another column

I have a dataframe where I want to create a new column based on an existing column where the values are non null.

The existing column is in decimals and some rows are null. I want to create a new column in integers.

I am using lambda but keep getting a syntax error. Could anyone tell me what's wrong? Thanks

df['new'] =  df['old'].apply(lambda x: int(x) if x>=0)

I also tried:

df['new'] =  df['old'].apply(lambda x: int(x) if x.isnull == False)

and this one:

df['new'] =  df['old'].apply(lambda x: x.astype(int) if x>=0)

The syntax error is pointing to the last close parenthesis.

df['new'] =  df['old'].apply(lambda x: int(x) if x>=0)

您需要在三元运算符的末尾添加一个else

df['new'] =  df['old'].apply(lambda x: int(x) if x>=0 else 'Nope')

You got syntax error, because your lambda function is not correct. Specifically, the if ... else ... conditional expression is wrong. The conditional expression must be

conditional_expression ::=  or_test [“if” or_test “else” expression]

You were missing else part.

Another thing I would like to mention is that the graceful way to convert the data type is to use astype function . If you want to cast data on some condition, you could do like:

new = df.loc[df.old>0].astype('int')

Then new would become a Series you need.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM