我的df就像这个
import pandas as pd
import numpy as np
df = pd.DataFrame({'Date data':['actual september 2019', 'September'],
'Date':['2019.12','2019.12']})
我想创建一个新的列,如果df['Date data']
是'September'
,我想写df['new_column'] = '2019.09'
预期输出为
import pandas as pd
import numpy as np
df = pd.DataFrame({'Date data':['actual september 2019', 'September'],
'Date':['2019.12','2019.12'], 'New_column':['2019.12','2019.09']})
我创建了一个可变
month = ('April', 'December', 'February', 'January', 'July-August', 'June', 'March', 'May', 'November', ' October', 'September')
和
df.loc[df['Date data'].isin(month) , 'new_col'] = '2019.9
IIUC,您可以使用:
from calendar import month_name
mapper = {m.lower(): f'{i:02d}' for i,m in enumerate(month_name)}
df['New_column'] = (df['Date data'].str.lower()
.map(mapper)
.radd('2019.')
.fillna(df['Date'])
)
示例:
Date data Date New_column
0 actual september 2019 2019.12 2019.12
1 September 2019.12 2019.09
2 ApRil 2019.12 2019.04
IIUC,您想要创建一个类似于Date
的新列。Date data
等于一个月的行我更改Date
中的列编号,并将其放在新列中。
如果所有月份都以大写字母开头(简单(:
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
df['New_column'] = df['Date']
for nr, month in enumerate(months):
df.loc[df['Date data'].eq(month), 'New_column'] = df['Date'].str[:5] + str(nr + 1).zfill(2)
如果月份可以有或没有大写字母,请使用.lower()
:
months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
df['New_column'] = df['Date']
for nr, month in months:
df.loc[df['Date data'].str.lower().eq(month), 'New_column'] = df['Date'].str[:5] + str(nr + 1).zfill(2)
输出:
Date data Date New_column
0 actual september 2019 2019.12 2019.12
1 September 2019.12 2019.09