Categories
Python Answers

How to fill missing values by mean in each group with Python Pandas?

Sometimes, we want to fill missing values by mean in each group with Python Pandas.

In this article, we’ll look at how to fill missing values by mean in each group with Python Pandas.

How to fill missing values by mean in each group with Python Pandas?

To fill missing values by mean in each group with Python Pandas, we can use the transform method.

For instance, we write

df["value"] = df.groupby("name").transform(lambda x: x.fillna(x.mean()))

to call groupby with 'name' to group by the name column.

And then we call transform with a lambda function that calls fillna with the mean of the values grouped by name.

Finally, we assign the returned values to the data frame df‘s value column.

Conclusion

To fill missing values by mean in each group with Python Pandas, we can use the transform method.

Categories
Python Answers

How to add missing dates to a Python Pandas dataframe?

Sometimes, we want to add missing dates to a Python Pandas dataframe, we call reindex.

In this article, we’ll look at how to add missing dates to a Python Pandas dataframe, we call reindex..

How to add missing dates to a Python Pandas dataframe?

To add missing dates to a Python Pandas dataframe, we call reindex.

For instance, we write

import pandas as pd

idx = pd.date_range('09-01-2013', '09-30-2013')

s = pd.Series({'09-02-2013': 2,
               '09-03-2013': 10,
               '09-06-2013': 5,
               '09-07-2013': 1})
s.index = pd.DatetimeIndex(s.index)

s = s.reindex(idx, fill_value=0)

to create a series s.

And then we call s.reindex with idx to fill the series s with the date range values.

And any missing values in the series are filled with 0 by setting the fill_value argument to 0.

Categories
Python Answers

How to convert index of a Python Pandas dataframe into a column?

Sometimes, we want to convert index of a Python Pandas dataframe into a column.

In this article, we’ll look at how to convert index of a Python Pandas dataframe into a column.

How to convert index of a Python Pandas dataframe into a column?

To convert index of a Python Pandas dataframe into a column, we assign the index value to the column.

For instance, we write

df['index1'] = df.index

to assign the index values we get with df.index to the 'index1‘ column.

Conclusion

To convert index of a Python Pandas dataframe into a column, we assign the index value to the column.

Categories
Python Answers

How to set value for particular cell in Python Pandas DataFrame using index?

Sometimes, we want to set value for particular cell in Python Pandas DataFrame using index.

In this article, we’ll look at how to set value for particular cell in Python Pandas DataFrame using index.

How to set value for particular cell in Python Pandas DataFrame using index?

To set value for particular cell in Python Pandas DataFrame using index, we assign the new value directly to the row and column we want to change.

For instance, we write

df.at['C', 'x'] = 10

to call at to get the item in column C and index x and set it to 10.

Conclusion

To set value for particular cell in Python Pandas DataFrame using index, we assign the new value directly to the row and column we want to change.

Categories
Python Answers

How to test if a string contains one of the substrings in a list, in Python Pandas?

Sometimes, we want to test if a string contains one of the substrings in a list.

In this article, we’ll look at how to test if a string contains one of the substrings in a list.

How to test if a string contains one of the substrings in a list, in Python Pandas?

To test if a string contains one of the substrings in a list, in Python Pandas, we call str.contains.

For instance, we write

s = pd.Series(['cat','hat','dog','fog','pet'])
df = pd.DataFrame(s)
df[s.str.contains('og|at')] 

to call s.str.contains with with a regex pattern string to return the items in data frame df that has the matched values.

Conclusion

To test if a string contains one of the substrings in a list, in Python Pandas, we call str.contains.