编辑cs列的每一行中的字符串

2024-10-06 12:38:06 发布

您现在位置:Python中文网/ 问答频道 /正文


我有一个带有日期列的csv,日期列为MM/DD/YY,但是我想将年份从00,02,03改为1900,1902,1903,这样它们就被列为MM/DD/YYYY

这就是我的工作原理:

df2['Date'] = df2['Date'].str.replace(r'00', '1900')

但我每年都要这样做直到68岁(也就是重复68次)。我不知道如何创建一个循环,以便在该范围内每年执行上述代码。我试过这个:

ogyear=00 
newyear=1900 
while ogyear <= 68:
    df2['date']=df2['Date'].str.replace(r'ogyear','newyear')
    ogyear += 1
    newyear += 1

但这将返回一个空数据集。还有别的办法吗?你知道吗

我不能使用datetime,因为它假定02引用的是2002而不是1902,当我试图将其编辑为日期时,python会显示一条错误消息,指出日期是不可变的,必须在原始数据集中更改。因为这个原因,我需要保持字符串的日期。我还附上了csv在这里的情况下,这是有帮助的。你知道吗


Tags: csv代码datereplacedd原理mmdf2
2条回答

我会这样做:

from datetime import datetime

# create a data frame with dates in format month/day/shortened year
d = pd.DataFrame({'dates': ['2/01/10','5/01/20','6/01/30']})

#loop through the dates in the dates column and add them 
#to list in desired form using datetime library,
#then substitute the dataframe dates column with the new ordered list

new_dates = []
for date in list(d['dates']):
    dat = datetime.date(datetime.strptime(date, '%m/%d/%y'))
    dat = dat.strftime("%m/%d/%Y")
    new_dates.append(dat)
new_dates
d['dates'] = pd.Series(new_dates)
d

我会这样做:

# create a data frame
d = pd.DataFrame({'date': ['20/01/00','20/01/20','20/01/50']})

# create year column
d['year'] = d['date'].str.split('/').str[2].astype(int) + 1900

# add new year into old date by replacing old year 
d['new_data'] = d['date'].str.replace('[0-9]*.$','') + d['year'].astype(str)

        date year   new_data
0   20/01/00 1900   20/01/1900
1   20/01/20 1920   20/01/1920
2   20/01/50 1950   20/01/1950

相关问题 更多 >