Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编辑csv列的每一行中的字符串_Python_String_Pandas_Csv_Date - Fatal编程技术网

Python 编辑csv列的每一行中的字符串

Python 编辑csv列的每一行中的字符串,python,string,pandas,csv,date,Python,String,Pandas,Csv,Date,我有一个带有日期列的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


我有一个带有日期列的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
ogyear=00
新年=1900

而我会这样做:

# 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

我会用下面的方法来做:

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