Retaining a column with all strings during groupby on a pandas dataframe

user308827
datetime    col_A   col_B
1/1/2012    125.501  A
1/2/2012    NaN      A
1/3/2012    125.501  A
1/4/2013    NaN      A
1/5/2013    125.501  B
2/28/2013   125.501  B
2/28/2014   125.501  B
1/2/2016    125.501  B
1/4/2016    125.501  B
2/28/2016   NaN      B

As per Fill in missing values in pandas dataframe using mean, I am filling in col_A missing values like this:

df = df.groupby([df.index.month, df.index.day]).transform(lambda x: x.fillna(x.mean()))

However, when I do this, it makes col_B go away. How can I retain col_B which is all strings?

jezrael

I think you can add col_A:

df['col_A'] = df.groupby([df.index.month, df.index.day])['col_A'].transform(lambda x: 
                                                                          x.fillna(x.mean()))
print df
              col_A col_B
datetime                 
2012-01-01  125.501     A
2012-01-02  125.501     A
2012-01-03  125.501     A
2013-01-04  125.501     A
2013-01-05  125.501     B
2013-02-28  125.501     B
2014-02-28  125.501     B
2016-01-02  125.501     B
2016-01-04  125.501     B
2016-02-28  125.501     B

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find and replace all the strings that match but case insensitive in a column of Pandas DataFrame

From Dev

Retaining nuisence coliumns in pandas groupby

From Dev

pandas add column to groupby dataframe

From Dev

Pandas dataframe groupby remove column

From Dev

pandas dataframe: subset by column + groupby another column

From Java

Pandas dataframe groupby make a list or array of a column

From Dev

Pandas : Assign result of groupby to dataframe to a new column

From Dev

Pandas multiplying a dataframe column with groupby result

From Dev

Advanced groupby column creation in pandas Dataframe

From Dev

Reorder all strings in a Column of a Dataframe R

From Dev

Exception during groupby pandas

From Dev

Convert column of date objects in Pandas DataFrame to strings

From Dev

How to add strings as a new column to Pandas Dataframe?

From Dev

Pandas Dataframe column with both Strings and Floats

From Dev

Filtering a column of lists of strings in a Pandas DataFrame

From Dev

Pandas - Concat strings after groupby in column, ignore NaN, ignore duplicates

From Dev

Remove certain strings from list of strings as column in pandas.DataFrame

From Dev

Pandas dataframe apply function to column strings based on other column value

From Dev

Delete all but one column of pandas dataframe?

From Dev

How to edit all values of a column in a pandas dataframe?

From Dev

Delete all but one column of pandas dataframe?

From Dev

Recode all but one value in pandas dataframe column

From Dev

Pandas combine strings with groupby

From Dev

Is it possible to groupBy a Spark's dataframe when not all values are present in column?

From Java

Pandas transform dataframe using groupby when count of a string in a column is maximum

From Dev

How to get a new dataframe with column count out of groupby function in pandas?

From Dev

Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby

From Dev

Pandas: Add new column with several values to groupby dataframe

From Dev

merge values of groupby results with dataframe in new column Python Pandas

Related Related

  1. 1

    Find and replace all the strings that match but case insensitive in a column of Pandas DataFrame

  2. 2

    Retaining nuisence coliumns in pandas groupby

  3. 3

    pandas add column to groupby dataframe

  4. 4

    Pandas dataframe groupby remove column

  5. 5

    pandas dataframe: subset by column + groupby another column

  6. 6

    Pandas dataframe groupby make a list or array of a column

  7. 7

    Pandas : Assign result of groupby to dataframe to a new column

  8. 8

    Pandas multiplying a dataframe column with groupby result

  9. 9

    Advanced groupby column creation in pandas Dataframe

  10. 10

    Reorder all strings in a Column of a Dataframe R

  11. 11

    Exception during groupby pandas

  12. 12

    Convert column of date objects in Pandas DataFrame to strings

  13. 13

    How to add strings as a new column to Pandas Dataframe?

  14. 14

    Pandas Dataframe column with both Strings and Floats

  15. 15

    Filtering a column of lists of strings in a Pandas DataFrame

  16. 16

    Pandas - Concat strings after groupby in column, ignore NaN, ignore duplicates

  17. 17

    Remove certain strings from list of strings as column in pandas.DataFrame

  18. 18

    Pandas dataframe apply function to column strings based on other column value

  19. 19

    Delete all but one column of pandas dataframe?

  20. 20

    How to edit all values of a column in a pandas dataframe?

  21. 21

    Delete all but one column of pandas dataframe?

  22. 22

    Recode all but one value in pandas dataframe column

  23. 23

    Pandas combine strings with groupby

  24. 24

    Is it possible to groupBy a Spark's dataframe when not all values are present in column?

  25. 25

    Pandas transform dataframe using groupby when count of a string in a column is maximum

  26. 26

    How to get a new dataframe with column count out of groupby function in pandas?

  27. 27

    Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby

  28. 28

    Pandas: Add new column with several values to groupby dataframe

  29. 29

    merge values of groupby results with dataframe in new column Python Pandas

HotTag

Archive