我在python上学习大熊猫,似乎无法完成这个问题。 2016年POPESTIMATE2010到POPESTIMATE有6个人口列,我需要找到这些年间人口变化最大的县。 (例如,如果5年期间的县人口为100,120,80,105,100,130,那么该期间的最大变化将是| 130-80 | = 50。)
到目前为止我所做的是设法将数据操作到数组和列表中,但我不确定哪个更好地解决了这个问题:
import numpy as np
def answer_seven():
sumlev = census_df.SUMLEV.values == 50
data = census_df[['POPESTIMATE2010', 'POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015', 'CTYNAME']].values[sumlev]
s = pd.Series(data[:, 0], [data[:, 1], data[:, 2], data[:, 3], data[:, 4], data[:, 5], data[:, 6]], dtype=np.int64)
return data
answer_seven()
返回数据时的输出:
array([[54660, 55253, 55175, ..., 55290, 55347, 'Autauga County'],
[183193, 186659, 190396, ..., 199713, 203709, 'Baldwin County'],
[27341, 27226, 27159, ..., 26815, 26489, 'Barbour County'],
...,
[21102, 20912, 20989, ..., 20903, 20822, 'Uinta County'],
[8545, 8469, 8443, ..., 8316, 8328, 'Washakie County'],
[7181, 7114, 7065, ..., 7185, 7234, 'Weston County']], dtype=object)
当我返回列表时,我得到一个列表:
55253 55175 55038 55290 55347 Autauga County 54660
186659 190396 195126 199713 203709 Baldwin County 183193
27226 27159 26973 26815 26489 Barbour County 27341
22733 22642 22512 22549 22583 Bibb County 22861
57711 57776 57734 57658 57673 Blount County 57373
10629 10606 10628 10829 10696 Bullock County 10887
20673 20408 20261 20276 20154 Butler County 20944
117768 117286 116575 115993 115620 Calhoun County 118437
33993 34075 34153 34052 34123 Chambers County 34098
26080 26023 26084 25995 25859 Cherokee County 25976
43739 43697 43795 43921 43943 Chilton County 43665
13593 13543 13378 13289 13170 Choctaw County 13841
25570 25144 25116 24847 24675 Clarke County 25767
13670 13456 13467 13538 13555 Clay County 13880
14971 14921 15028 15072 15018 Cleburne County 14973
50448 51173 50755 50831 51211 Coffee County 50177
54443 54472 54471 54480 54354 Colbert County 54514
13121 12996 12875 12662 12672 Conecuh County 13208
11348 11195 11059 10807 10724 Coosa County 11758
38060 37818 37830 37888 37835 Covington County 37796
13896 13951 13932 13948 13963 Crenshaw County 13853
80469 80374 80756 81221 82005 Cullman County 80473
50109 50324 49833 49501 49565 Dale County 50358
43178 42777 42021 41662 41131 Dallas County 43803
71387 70942 70869 71012 71130 DeKalb County 71142
80012 80432 80883 81022 81468 Elmore County 79465
38213 38034 37857 37784 37789 Escambia County 38309
104236 104235 103852 103452 103057 Etowah County 104442
17062 16960 16857 16842 16759 Fayette County 17231
31729 31648 31507 31592 31696 Franklin County 31734
...
我看了很多论坛帖子,但我发现任何与此无关的内容。我知道最好的方法是创建一个“最高”列和一个“最低”列,然后找到差异最大的县,但我不知道如何找到一个值的最大/最小值阵列。真的很感激帮助!
答案 0 :(得分:2)
鉴于您提到的数据(仅限于几行用于演示目的),我们首先将其转换为适当的DataFrame:
from io import StringIO
dataset = """\
55253 55175 55038 55290 55347 Autauga County 54660
186659 190396 195126 199713 203709 Baldwin County 183193
27226 27159 26973 26815 26489 Barbour County 27341
22733 22642 22512 22549 22583 Bibb County 22861
57711 57776 57734 57658 57673 Blount County 57373
"""
df = pd.DataFrame.from_csv(StringIO(dataset), sep='\s{2,}', header=None).reset_index()
df.columns = ['y1', 'y2', 'y3', 'y4', 'y5', 'name', 'y6']
df = df.set_index('name')
df.head()
y1 y2 y3 y4 y5 y6
name
Autauga County 55253 55175 55038 55290 55347 54660
Baldwin County 186659 190396 195126 199713 203709 183193
Barbour County 27226 27159 26973 26815 26489 27341
Bibb County 22733 22642 22512 22549 22583 22861
Blount County 57711 57776 57734 57658 57673 57373
然后,您可以使用numpy的min
和max
方法计算数据集中的最小值和最大值。之后,您可以创建一个由最大差异组成的新DataFrame。与pandas或numpy中的优化方法相比,python中不需要任何循环。
df2 = DataFrame((np.max(df.values, axis=1) - np.min(df.values, axis=1)), index=df.index, columns=['largest_diff'])
df2.head()
largest_diff
name
Autauga County 687
Baldwin County 20516
Barbour County 852
Bibb County 349
Blount County 403
答案 1 :(得分:1)
如果您的数据首先在pandas数据框中,那么请使用pandas min()和max()方法:
>>> df1
year: 2010 2011 2012 2013 2014
city
abilene 47000 2000 31000 72000 47000
boise 44000 55000 68000 17000 63000
calgary 39000 86000 6000 97000 1000
denver 57000 52000 46000 0 43000
>>> df1.T.max()-df1.T.min()
city
abilene 70000
boise 51000
calgary 96000
denver 57000
dtype: int32
答案 2 :(得分:0)
这是我的天真实施。
maxchange = (None,0)
for row in data:
low = min(row[:-1])
high = max(row[:-1])
if high-low > maxchange[1]:
maxchange = (row[-1], high-low)
print(maxchange)
这使用data
中创建的answer_seven
数组。这只是找到每个县的最小值和最大值,并找出各县之间的最大差异。
答案 3 :(得分:0)
试试这个:
def df_max_dif (x):
max_dif = 0
for ind in x.index:
max_value = np.max(np.abs(x-x.loc[ind]))
if max_value > max_dif:
max_dif = max_value
return max_dif
df['max_dif'] = np.nan
for indx in df.index:
df.loc[indx,'max_dif'] = df_max_dif(df.loc[indx].drop('max_dif'))
希望它有所帮助!
答案 4 :(得分:0)
我认为这应该可以解决您的问题
temp = census_df[census_df['SUMLEV'] == 50].set_index('CTYNAME')
yrs = ['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013', 'POPESTIMATE2014', 'POPESTIMATE2015']
res = temp.loc[:,yrs].max(axis=1) - temp.loc[:,yrs].min(axis=1)
res.idxmax()
答案 5 :(得分:0)
def my_idea():
columns_to_keep = ['POPESTIMATE2015','POPESTIMATE2014','POPESTIMATE2013','POPESTIMATE2012','POPESTIMATE2011','POPESTIMATE2010']
copy_df = census_df[columns_to_keep]
# max_difference_per_country is a Series with sorted values from high to low
max_difference_per_country = (copy_df.max(axis=1) - copy_df.min(axis=1)).sort_values(ascending=False)
# get its index
index_of_max_difference_per_country = max_difference_per_country.first_valid_index()
return census_df['CTYNAME'].iloc[index_of_max_difference_per_country]
答案 6 :(得分:0)
@Tolis提供的答案是不排除国家,并给出“ Texas”作为结果。正确的代码应如下所示:
def answer_seven():
columns_to_keep = ['POPESTIMATE2015','POPESTIMATE2014','POPESTIMATE2013','POPESTIMATE2012','POPESTIMATE2011','POPESTIMATE2010']
rows_to_keep = census_df[census_df['SUMLEV'] == 50]
copy_df = rows_to_keep[columns_to_keep]
# max_difference_per_country is a Series with sorted values from high to low
max_difference_per_country = (copy_df.max(axis=1) - copy_df.min(axis=1)).sort_values(ascending=False)
# get its index
index_of_max_difference_per_country = max_difference_per_country.first_valid_index()
return census_df['CTYNAME'].iloc[index_of_max_difference_per_country]
答案 7 :(得分:0)
def answer_seven():
county = census_df[census_df['SUMLEV']==50]
county= county.set_index('CTYNAME')
req_col = ['POPESTIMATE2010',
'POPESTIMATE2011',
'POPESTIMATE2012',
'POPESTIMATE2013',
'POPESTIMATE2014',
'POPESTIMATE2015']
countyP= county[req_col]
res = (countyP[req_col].max(axis=1) - countyP[req_col].min(axis=1)).nlargest(1)
return res.argmax()
answer_seven()
答案 8 :(得分:0)
def answer_seven():
cols = [ 'POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015' ]
new = census_df[ census_df['SUMLEV']==50 ].set_index('CTYNAME').apply( lambda x: np.max( x[cols] - np.min( x[cols]) ), axis=1)
return new.idxmax()
答案 9 :(得分:0)
def answer_seven():
temp=[];
df=census_df.groupby('STNAME')
df=df.sum()
val=df[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']]
max_val=val.max(axis=1)
min_val=val.min(axis=1)
fd=max_val-min_val;
fd=fd[fd.values==fd.values.max()]
return fd.index[0]
答案 10 :(得分:0)
def answer_seven():
df=census_df[ census_df['SUMLEV']==50 ]
df['Max']=df[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].max(axis=1)
df['Min']=df[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].min(axis=1)
df['Diff']= df['Max'] - df['Min']
max_val_idx=df['Diff'].idxmax()
return df.loc[max_val_idx]['CTYNAME']
answer_seven()
答案 11 :(得分:0)
def answer_seven():
max= census_df[['POPESTIMATE2010', 'POPESTIMATE2011', 'POPESTIMATE2012','POPESTIMATE2013', "POPESTIMATE2014", "POPESTIMATE2015"]].max(axis=1)
min= census_df[['POPESTIMATE2010', 'POPESTIMATE2011', 'POPESTIMATE2012', 'POPESTIMATE2013', "POPESTIMATE2014", "POPESTIMATE2015"]].min(axis=1)
absolute_diff = (max-min).abs()
absolute_diff.index = census_df.index
census_df['absolute_diff'] = absolute_diff
return census_df.loc[census_df[census_df['SUMLEV'] == 50]['absolute_diff'].idxmax(), 'CTYNAME']
答案 12 :(得分:-1)
set jb_column [llength [array names jb_node 274,*]
puts "The row 274 has $jb_column"
puts $jb_code(274,75)