我有一个按日期索引的数据框,以便仅将某些年份输出到csv。我需要将2列连接到一个新列。但是,连接列的输出的前18行已更改为日期格式。
dates=[]
row=[]
col=[]
GHB_heads=[]
HEAD=[]
flux=[]
with open (fileloc, 'r') as fin:
#print("reading in data")
data=fin.readlines()[1:]
for line in data:
vals=line.split()
date=vals[0][:10]
dates.append(date)
row.append(int(vals[6]))
col.append(int(vals[7]))
GHB_heads.append(float(vals[9]))
HEAD.append(float(vals[10]))
flux.append(float(vals[11]))
df=pd.DataFrame(list(zip(dates,row, col,GHB_heads,HEAD,flux)),columns=['Date','row','col','GHB_heads','HEAD','flux'])
df['row_col']= df["row"].astype(str) + "-" + df["col"].astype(str)
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
y2016 = df[df['Date'].dt.year == 2016]
y2067 = df[df['Date'].dt.year == 2067]
y2016.to_csv('GHB_LY9_2016.csv')
y2067.to_csv('GHB_LY9_2067.csv')