I am training on pandas and how to sum a series in a DataFrame. And I could use two ways using list and normal variable. The code is like that
import pandas as pd
url = 'http://bit .ly/imdbratings'
df = pd.read_csv(url , chunksize=250)
result = []
for chunk in df:
result.append(sum(chunk['duration']))
print(sum(result))
The code is working well and the output is 118439
And when using a variable instead of list like that
import pandas as pd
url = 'http://bit .ly/imdbratings'
df = pd.read_csv(url , chunksize=250)
total = 0
for chunk in df:
total += sum(chunk['duration'])
print(total)
The output is the same 118439
** The problem is when trying the both approaches in one code like that
import pandas as pd
url = 'http://bit .ly/imdbratings'
df = pd.read_csv(url, chunksize=250)
result = []
for chunk in df:
result.append(sum(chunk['duration']))
print(sum(result))
total = 0
for chunk in df:
total += sum(chunk['duration'])
print(total)
I got the result for the first approach but got 0 for the total
variable. Any ideas why I got 0 when combining the two approaches?
** Remove the space in the url.