i have data set. i want to save my data in df2.
RegionNo | Internet |
---|---|
1 | 1 |
1 | 2 |
1 | 3 |
1 | 4 |
2 | 1 |
2 | 2 |
2 | 3 |
expected :
RegionNo | Internet |
---|---|
1 | 10 |
2 | 6 |
def accu():
k = 0
for i in range(len(df) - 1):
if df.loc[i, "RegionNo"] == df.loc[i 1, "RegionNo"]:
df2.loc[k, "Internet"] = df2.loc[k, "Internet"] df.loc[i, "Internet"]
df2.loc[k, "RegionNo"] = df.loc[i, "RegionNo"]
else:
k = k 1
df2.loc[k, "RegionNo"] = df.loc[i 1, "RegionNo"]
df2.loc[k, "Internet"] = df2.loc[k, "Internet"] df.loc[i, "Internet"]
but every time i try my code, keyError occured: keyerror : 0
File "C:/Users/Monet/PycharmProjects/pythonProject1/new_main.py", line 9, in accu
df2.loc[k, "Internet"] = df2.loc[k, "Internet"] df.loc[i, "Internet"]
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 925, in __getitem__
return self._getitem_tuple(key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1100, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 838, in _getitem_lowerdim
section = self._getitem_axis(key, axis=i)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1164, in _getitem_axis
return self._get_label(key, axis=axis)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1113, in _get_label
return self.obj.xs(label, axis=axis)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 3776, in xs
loc = index.get_loc(key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 0
CodePudding user response:
You wanted:
print(df.groupby('RegionNo').sum())
For example:
from pandas import DataFrame
data = [
{'RegionNo': 1, 'Internet': 1},
{'RegionNo': 1, 'Internet': 2},
{'RegionNo': 1, 'Internet': 3},
{'RegionNo': 1, 'Internet': 4},
{'RegionNo': 2, 'Internet': 1},
{'RegionNo': 2, 'Internet': 2},
{'RegionNo': 2, 'Internet': 3}
]
df = DataFrame(data)
df2 = df.groupby('RegionNo').sum()
print(df2)
Result:
Internet
RegionNo
1 10
2 6
You haven't shared how you created df2
, but it likely doesn't have the position you're trying to access, which causes the error you're asking about. (by the way, it helps if you actually ask a clear question at the end of the explanation in the question text, so people don't have to drag it out). And as the code above shows, you don't need to access df2
that way either.