[英]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. 我的数据帧具有与重复的值的月柱Apr
, Apr.1
, Apr.2
等因为没有年列。 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: 我使用for循环根据月值添加了year列,如下所示,但我想找到一种更有效的方法:
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. 您可以使用.str
并将整列视为字符串,以在点处分割。 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: 现在df
是:
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 您可以在拆分后使用pd.to_numeric
并添加2015
即
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.