4848

Group by min and fill NAs with value from another column

I have a sample data frame that looks like this.

df = pd.DataFrame (data = {'uid': [1,1,1,2,2,3], 'pagename':['home', 'blah', 'blah', 'home', 'blah', 'blah'], 'startpage': ['NA', 'NA', 'NA', 'home', 'home', 'blah'], 'date_time': [0,1,2,5,9,1]})

What I want to do is group by the UID and find the min date_time. If the startpage of the min date_time is Null (I put string 'NA' for Null) then I want to use the pagename from that row to populate the startpage column. I also want the startpage to be populated for all rows with the same UID.

This is the ending dataframe that I want.

df = pd.DataFrame (data = {'uid': [1,1,1,2,2,3], 'pagename':['home', 'blah', 'blah', 'home', 'blah', 'blah'], 'startpage': ['home', 'home', 'home', 'home', 'home', 'blah'], 'date_time': [0,1,2,5,9,1]})

Answer1:

fillna with transform

i = df.groupby('uid').date_time.transform('idxmin') df.startpage = df.startpage.fillna(i.map(df.pagename)) print(df) date_time pagename startpage uid 0 0 home home 1 1 1 blah home 1 2 2 blah home 1 3 5 home home 2 4 9 blah home 2 5 1 blah blah 3

Recommend

  • How do I do a simple isPulledToRefresh in Xamarin forms?
  • rest api unauthorized Azure Data Catalog
  • Convert Html into PDF using webview : can not create multiple pages
  • Struts2 jquery datepicker passing 0 or null value to action class
  • maps in shared memory: Boost.Interprocess demo fails due to unmet date_time dependency
  • Selenium - Best Practices - Page Object Patter & Page Factory
  • strtotime was not working in php with m-d-Y format in php? [duplicate]
  • if clause in bigquery
  • How to do popup on new site for 301 redirected users?
  • $this->params returns null in cakephp model
  • How to calculate total across columns but one?
  • JPA/Hibernate - Entity name seems to be important. If I rename to “Bob” works fine
  • firebase, how to update data at a key
  • Attemping to change attributes of $(this) inside success function
  • pandas computation in each group
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Can my PDF ping my server when it is opened?
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • For loop with if condition on multiple R functions
  • Plotting line graph with factors in R
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • D3 get axis values on zoom event
  • wxPython: displaying multiple widgets in same frame
  • rspec simple example getting error on request variable in integration test
  • Custom validator control occupying space even though display set to dynamic
  • JSON response opens as a file, but I can't access it with JavaScript
  • Test if a set exists before trying to drop it
  • Android full screen on only one activity?
  • R - Combining Columns to String Based on Logical Match
  • Read text file and split every line in MSBuild
  • Accessing IRQ description array within a module and displaying action names
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • embed rChart in Markdown
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • Unable to use reactive element in my shiny app