简体   繁体   中英

Map dataframe column value by another column's value

My dataframe has a month column with values that repeat as Apr , Apr.1 , Apr.2 etc. because there is no year column. I added a year column based on the month value using a for loop as shown below, but I'd like to find a more efficient way to do this:

Products['Year'] = '2015'
for i in range(0, len(Products.Month)):
    if '.1' in Products['Month'][i]:
        Products['Year'][i] = '2016'
    elif '.2' in Products['Month'][i]:
        Products['Year'][i] = '2017'

You can use .str and treat the whole columns like string to split at the dot. Now, apply a function that takes the number string and turns into a new year value if possible.

Starting dataframe:

   Month
0    Apr
1  Apr.1
2  Apr.2

Solution:

def get_year(entry):
    value = 2015
    try:
        value += int(entry[-1])
    finally:
        return str(value)

df['Year'] = df.Month.str.split('.').apply(get_year)

Now df is:

   Month  Year
0    Apr  2015
1  Apr.1  2016
2  Apr.2  2017

You can use pd.to_numeric after splitting and add 2015 ie

df['new'] = pd.to_numeric(df['Month'].str.split('.').str[-1],errors='coerce').fillna(0) + 2015

# Sample DataFrame from @ Mike Muller
   Month  Year     new
0    Apr  2015  2015.0
1  Apr.1  2016  2016.0
2  Apr.2  2017  2017.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM