Set value for particular cell in pandas DataFrame using index

RukTech’s answer, df.set_value(‘C’, ‘x’, 10), is far and away faster than the options I’ve suggested below. However, it has been slated for deprecation. Going forward, the recommended method is .iat/.at. Why df.xs(‘C’)[‘x’]=10 does not work: df.xs(‘C’) by default, returns a new dataframe with a copy of the data, so df.xs(‘C’)[‘x’]=10 modifies this new dataframe only. … Read more

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

Quick Answer: The simplest way to get row counts per group is by calling .size(), which returns a Series: df.groupby([‘col1′,’col2’]).size() Usually you want this result as a DataFrame (instead of a Series) so you can do: df.groupby([‘col1’, ‘col2’]).size().reset_index(name=”counts”) If you want to find out how to calculate the row counts and other statistics for each … Read more

How do I count the NaN values in a column in pandas DataFrame?

Use the isna() method (or it’s alias isnull() which is also compatible with older pandas versions < 0.21.0) and then sum to count the NaN values. For one column: >>> s = pd.Series([1,2,3, np.nan, np.nan]) >>> s.isna().sum() # or s.isnull().sum() for older pandas versions 2 For several columns, this also works: >>> df = pd.DataFrame({‘a’:[1,2,np.nan], … Read more

How to filter Pandas dataframe using ‘in’ and ‘not in’ like in SQL

You can use pd.Series.isin. For “IN” use: something.isin(somewhere) Or for “NOT IN”: ~something.isin(somewhere) As a worked example: import pandas as pd >>> df country 0 US 1 UK 2 Germany 3 China >>> countries_to_keep [‘UK’, ‘China’] >>> df.country.isin(countries_to_keep) 0 False 1 True 2 False 3 True Name: country, dtype: bool >>> df[df.country.isin(countries_to_keep)] country 1 UK … Read more

Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

The or and and python statements require truth-values. For pandas, these are considered ambiguous so you should use “bitwise” | (or) or & (and) operations: df = df[(df[‘col’] < -0.25) | (df[‘col’] > 0.25)] These are overloaded for these kinds of data structures to yield the element-wise or or and. Just to add some more … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)