Create an function from data-frame column values and insert into blank elements of another column



  • I have a data-frame (df) which looks like:

              FHE
    0           1
    1           1
    2           1
    3
    4           1
    5  0.77027027
    
    

    I am trying to create a new column called FHE_TO_USE which copies the FHE column and creates the mean of the FHE column and fills in any blanks with the mean. So the solution would look like:

            FHE     FHE_TO_USE
    0           1            1
    1           1            1
    2           1            1
    3               0.95405405
    4           1            1
    5  0.77027027   0.77027027
    
    

    I've tried the folling without success:

    df["FHE"] = np.where(df["PATTERN"]=="FT",1,df["FHE"])
    
    df["FHE"] = df["FHE"].convert_objects(convert_numeric=True)
    
    df["FHE_TO_USE"] = np.where(df["FHE"]=="",statistics.mean(df["FHE"]),df["FHE"])
    
    

    This also gives me a future warning:

    FutureWarning: convert_objects is deprecated.  To re-infer data dtypes for object columns, use Series.infer_objects()
    For all other conversions use the data-type specific converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.
    
    

    What is the correct way to create the mean?



  • You can use the function fillna():

    df['FHE'] = pd.to_numeric(df.FHE, errors='coerce')
    df['FHE_TO_USE'] = df.FHE.fillna(df.FHE.mean())
    
    

    Result:

           FHE  FHE_TO_USE
    0  1.00000    1.000000
    1  1.00000    1.000000
    2  1.00000    1.000000
    3      NaN    0.954054
    4  1.00000    1.000000
    5  0.77027    0.770270
    
    


最新帖子

最新内容

  • S

    Like this: [NSThread isMainThread]

    read more
  • S

    I'm pretty sure that ^/[p]/* is going to match any path that starts with /p, and you have it as the first rule, so it matches first and allows access.

    The square brackets [] are defining a set of characters to match, you only want to match one so you don't need them. The * says to match zero or more / characters at the end, you know there will be one / after p so omit the star. The path is a prefix so you don't need to worry about the variable part of the path.

    Try this:

    - { path: ^/p/, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/profile, role: ROLE_CONNECT }

    read more
  • S

    I want :

    /p/{name} ==> in public access
    /profile ==> in loggin access

    I did this in security.yml

    access_control: - { path: ^/[p]/* , role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/*, role: ROLE_CONNECT }

    But, when I browse :

    /profile => accessible in public ==> K.O.
    /p/name-here => accessible in public ==> OK

    read more
  • S

    This question already has an answer here:

    Check whether or not the current thread is the main thread 11 answers

    How to determine if the current thread inside a method is a main thread, or just a thread that is asynchronous and created? Does the ID tell me?

    read more
  • S

    Before you push() in your set<int> save the value in int max in global variable

    read more

推荐阅读