Daniel
Daniel

Reputation: 5381

apply values to pandas column

Having a dataframe as

    dasz_id     sector    counts
0   0           dasz_id   2011.0
1   NaN         wah11     0.0
2   NaN         wah21     0.0
3   0           dasz_id   2012.0
4   NaN         wah11     0.0
5   NaN         wah21     0.0

I'm trying to get the daz_id value and apply it to all the rows until a new dasz value is appear, so the desired output would look as:

    dasz_id     sector    counts
0   2011        dasz_id   2011.0
1   2011        wah11     0.0
2   2011        wah21     0.0
3   2012        dasz_id   2012.0
4   2012        wah11     0.0
5   2012        wah21     0.0

I've created a function using the apply method, which works to get the value over, but i don't know how to apply the values for the rest of the rows. What am i doing wrong?

def dasz(row):
    if row.sector == "dasz_id":
        return int(row.counts)
    else:
        #get previous dasz_id  value

e["dasz_id"] = e.apply(dasz, axis = 1)

Upvotes: 0

Views: 32

Answers (2)

Sachit Nagpal
Sachit Nagpal

Reputation: 486

Using the dasz function that you created, and ffill function used by Wen, you could also do:

def dasz(row):
    if row.sector == "dasz_id":
        return row.counts

e["dasz_id"] = e.apply(dasz, axis = 1)
e.ffill(inplace=True)

Upvotes: 1

BENY
BENY

Reputation: 323226

I do not know why you have duplicated index, but here is one way

df['dasz_id'] = df['counts']
df['dasz_id'] = df['dasz_id'].replace({0:np.nan}).ffill()

df
Out[84]: 
   dasz_id   sector  counts
0   2011.0  dasz_id  2011.0
1   2011.0    wah11     0.0
2   2011.0    wah21     0.0
0   2012.0  dasz_id  2012.0
1   2012.0    wah11     0.0
2   2012.0    wah21     0.0

Upvotes: 2

Related Questions