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 是一个 Series:
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函数返回一个单一的结果(我测试过了),那么为什么zed是DataFrame? pop_50如何返回一个系列?它需要一行(其中facts['name'] == name)并从中取出单个值(在population列下),然后将其称为pop。它然后对perc执行相同的想法。new_pop是两个单独值的数学组合,因此它也是一个单独的值,并且该函数仅返回该值,难道不是吗?
谢谢。
z
的内容吗?在我的测试中,它是一个Pandas系列对象。 - Jamesnew_pop
是一个序列。尝试返回一个整数。例如new_pop.population.values[0]
。 - Jan Zeiseweis