pandas:根据其他变量的值获取列的值

时间:2013-12-18 06:37:07

标签: python pandas

我正在使用商业机构数据集。这是一个宽格式的小组,每年的就业人数,比如2005年,2006年,2007年等。有一个变量,一年的业务搬到了一个新的位置,比如2006年。我想创建一个变量for移动年度的具体就业 - 也就是说,如果移动年份为x,则查找第x年的就业价值。

理想情况下,我会对此进行矢量化。这就是我现在所拥有的,但我担心索引不够通用/可能是危险的,我可能会在实际数据中得到意想不到的结果。

import pandas as pd
import numpy as np
np.random.seed(43)

## prep mock data
N = 100
industry = ['utilities','sales','real estate','finance']
city = ['sf','san mateo','oakland']
move = np.arange(2006,2010)
ind = np.random.choice(industry, N)
cty = np.random.choice(city, N)
moveyr = np.random.choice(move, N)

## place it in dataframe
jobs06 = np.random.randint(low=1,high=250,size=N)
jobs06 = np.random.randint(low=1,high=250,size=N)
jobs07 = np.random.randint(low=1,high=250,size=N)
jobs08 = np.random.randint(low=1,high=250,size=N)
jobs09 = np.random.randint(low=1,high=250,size=N)


df_city =pd.DataFrame({'industry':ind,'city':cty,'moveyear':moveyr,'jobs06':jobs06,'jobs07':jobs07,'jobs08':jobs08,'jobs09':jobs09})

df_city.head()

提供此数据:

+---+------------+------------+--------+--------+--------+--------+----------+
|   |    city    |  industry  | jobs06 | jobs07 | jobs08 | jobs09 | moveyear |
+---+------------+------------+--------+--------+--------+--------+----------+
| 0 |  sf        |  utilities |    206 |     82 |    192 |    236 |     2009 |
| 1 |  oakland   |  utilities |     10 |    244 |      2 |      7 |     2007 |
| 2 |  san mateo |  finance   |    182 |    164 |     49 |     66 |     2006 |
| 3 |  oakland   |  sales     |     27 |    228 |     33 |    169 |     2007 |
| 4 |  san mateo |  sales     |     24 |     24 |    127 |    165 |     2007 |
+---+------------+------------+--------+--------+--------+--------+----------+

如果我做这样的事情,我会得到一些似乎是正确的东西,至少在这个玩具示例中,但我不肯定这是a)安全,索引,b)'正确'的pythonic方式(以及无论如何大熊猫相当于那个词。)

df_city['moveyearemp']=0  ## seemingly must declare first
for count, row  in df_city.head(5).iterrows(): 
    get_moveyear_emp = 'jobs' + str(row['moveyear'])[2:]
    ## is this 'proper' indexing?
    df_city.ix[count,'moveyearemp'] = df_city.ix[count,get_moveyear_emp]
print df_city['moveyearemp'].head()

这确实给出了预期的结果 - 例如,236确实是2009年第一排/业务的就业;第二排等于2007年的244同上。

0    236
1    244
2    182
3    228
4     24
Name: moveyearemp, dtype: int64

2 个答案:

答案 0 :(得分:2)

我可能会迭代这些年(因为年数少于行数):

In [11]: df_city.moveyear.unique()
Out[11]: array([2009, 2007, 2006, 2008])

这是一种方法,但我不认为我会称之为 pandastic ......

g = df_city.groupby('moveyear')
df_city['moveyearemp'] = 0
for year, ind in g.indices.iteritems():
    year_abbr = str(year)[2:]
    df_city.loc[ind, 'moveyearemp'] = df_city.loc[ind, 'jobs%s' % year_abbr]

你得到:

In [21]: df_city.head()
Out[21]: 
        city   industry  jobs06  jobs07  jobs08  jobs09  moveyear  moveyearemp
0         sf  utilities     206      82     192     236      2009          236
1    oakland  utilities      10     244       2       7      2007          244
2  san mateo    finance     182     164      49      66      2006          182
3    oakland      sales      27     228      33     169      2007          228
4  san mateo      sales      24      24     127     165      2007           24

答案 1 :(得分:0)

如果您预先计算moveyearemp数据框(按年份编制索引的数据集),您将可以执行df_city.join(moveyearemp, on='year')