pandas: combine two columns in a DataFrame

后端 未结 5 509
感情败类
感情败类 2020-11-30 07:42

I have a pandas DataFrame that has multiple columns in it:

Index: 239897 entries, 2012-05-11 15:20:00 to 2012-06-02 23:44:51
Data columns:
foo           


        
5条回答
  •  情书的邮戳
    2020-11-30 08:00

    Another option, use the .apply() method on the frame. You can do reassign a column with deference to existing data...

    import pandas as pd
    import numpy as np
    
    # get your data into a dataframe
    
    # replace content in "bar" with "foo" if "bar" is null
    df["bar"] = df.apply(lambda row: row["foo"] if row["bar"] == np.NaN else row["bar"], axis=1) 
    
    # note: change 'np.NaN' with null values you have like an empty string
    

提交回复
热议问题