Posts

Showing posts with the label pandas-groupby

IndexError: single positional indexer is out-of-bounds for my Pandas Dataframe

IndexError: single positional indexer is out-of-bounds for my Pandas Dataframe I am trying to perform certain addition operation to the columns in my dataframe and then store the values within the same dataframe. My DataFrame has 3796 rows and 11 columns containing data from the year 1928 till 2000, with weekly data. I want to take a weighted average of the data points(depending upon their dates) and store the values in the same dataframe. My code is as follows: import pandas as pd import numpy as np df=pd.read_excel("D:SUMMER INTERNSHIP CONCORDIAUpdated Work hereKitsim Reservoir (Number 220)Book3_Final.xlsx") a=0 b=0 k=20 p=3 for z in range(3,11): a=0 b=0 for x in range(1928,2000): for y in range(0,12): if df.iloc[a,1]==1: df.iloc[b,k]=(7*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+3*df.iloc[a+4,z])/31 a=a+4 b=b+1 if df.iloc[a,1]==2: ...

select index value from groupby on a pandas dataframe in python

select index value from groupby on a pandas dataframe in python I have the following dataframe: df = pd.DataFrame({'place' : ['A', 'B', 'C', 'D', 'E', 'F'], 'population': [10 , 20, 30, 15, 25, 35], 'region': ['I', 'II', 'III', 'I', 'II', 'III']}) And it looks like this: place population region 0 A 10 I 1 B 20 II 2 C 30 III 3 D 15 I 4 E 25 II 5 F 35 III I would like to select the place with the smallest population from the region with the highest population. df.groupby('region').population.sum() Returns: region I 25 II 45 III 65 Name: population, dtype: int64 But I have no clue how to proceed from here (using .groupby / .loc / .iloc) Any suggestion? 2 Answers ...