In the folwwing code:
import pandas as pd
import sqlite3
import math
import numpy
con = sqlite3.connect(r'C:\Python34\factbook.db')
facts = pd.read_sql_query('select * from facts;', con)
facts.dropna(inplace=True)
facts = facts[facts['area_land']!=0][:]
facts = facts[facts['population']!=0][:]
facts.reset_index(drop=True, inplace=True)
def pop_50(name):
pop = facts[facts['name'] == name]['population']
perc = facts[facts['name'] == name]['population_growth']
new_pop = pop*(math.e**(35*perc))
return new_pop
x=pd.Series(data=facts['name'])
z = x.apply(pop_50)
x是一個系列:適用()返回一個數據幀,而不是
0 Afghanistan
1 Albania
2 Algeria
3 Andorra
4 Angola
5 Antigua and Barbuda
6 Argentina
7 Armenia
等等...
但是z是不是。這裏是一個鏈接,看看它是什麼(一個數據幀): https://www.scribd.com/document/357697929/Doc1
我不明白爲什麼。 pop_50函數返回單個結果(我測試過),那麼爲什麼需要一個DataFrame呢? pop_50如何返回一個系列?它需要一行(where facts ['name'] == name),並從中獲得一個值(在人口列下),而不是稱之爲彈出。它比perc做同樣的想法。 new_pop是2個singel值的數學組合,所以它也是一個單一的值,func只是返回值,不是嗎?
謝謝。
你可以發佈'z'的一些內容嗎?在我的測試中,它是一個熊貓系列對象。 – James
這是因爲你的返回值'new_pop'是一個系列。嘗試返回一個整數。像'new_pop.population.values [0]'。 –