How to access subdataframes of pandas groupby by key

You can use the get_group method: In [21]: gb.get_group(‘foo’) Out[21]: A B C 0 foo 1.624345 5 2 foo -0.528172 11 4 foo 0.865408 14 Note: This doesn’t require creating an intermediary dictionary / copy of every subdataframe for every group, so will be much more memory-efficient than creating the naive dictionary with dict(iter(gb)). This … Read more

Error “‘DataFrame’ object has no attribute ‘append'”

As of pandas 2.0, append (previously deprecated) was removed. You need to use concat instead (for most applications): df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True) As noted by @cottontail, it’s also possible to use loc, although this only works if the new index is not already present in the DataFrame (typically, this will be the case if … Read more

Get a frequency count based on multiple dataframe columns

You can use groupby’s size import pandas as pd # load the sample data data = {‘Group’: [‘Short’, ‘Short’, ‘Moderate’, ‘Moderate’, ‘Tall’], ‘Size’: [‘Small’, ‘Small’, ‘Medium’, ‘Small’, ‘Large’]} df = pd.DataFrame(data) Option 1: dfg = df.groupby(by=[“Group”, “Size”]).size() # which results in a pandas.core.series.Series Group Size Moderate Medium 1 Small 1 Short Small 2 Tall Large … Read more

Replace NaN in one column with value from corresponding row of second column

Assuming your DataFrame is in df: df.Temp_Rating.fillna(df.Farheit, inplace=True) del df[‘Farheit’] df.columns=”File heat Observations”.split() First replace any NaN values with the corresponding value of df.Farheit. Delete the ‘Farheit’ column. Then rename the columns. Here’s the resulting DataFrame: File heat Observations 0 1 YesQ 75 1 1 NoR 115 2 1 YesA 63 3 1 NoT 41 … Read more

How do I create a new column where the values are selected based on existing columns?

If you only have two choices to select from then use np.where: df[‘color’] = np.where(df[‘Set’]==’Z’, ‘green’, ‘red’) For example, import pandas as pd import numpy as np df = pd.DataFrame({‘Type’:list(‘ABBC’), ‘Set’:list(‘ZZXY’)}) df[‘color’] = np.where(df[‘Set’]==’Z’, ‘green’, ‘red’) print(df) yields Set Type color 0 Z A green 1 Z B green 2 X B red 3 Y … Read more

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