第一列是日期,随后几列是节点(6100)。当我读取csv时,值在一列中(添加了图片)。因此,我按空格分隔并添加了标题。每个节点具有对应于一个日期的4个值,但是数据将在日期下填充。
功能:
Date 1 2 3 4 ...
2/12 14 14 14 14
14 13 13 13 nan
14 13 13 13 nan
14 13 13 13 nan
我想要得到的东西
Date 1 2 3 4 ...
2/12 14 14 14 14
14 13 13 13
14 13 13 13
14 13 13 13
2/13 ......
我对此非常困惑。任何建议都会有所帮助。预先感谢:)
path =r'Paco' # use your path
#filenames = glob.glob(path + "/*_drh.csv")
filenames = glob.glob(path + "/FM2Sim_GW_HeadAllOut.txt")
filename ={}
for filename in filenames:
if len(filenames) == 1:
#The setup
column_name = np.arange(0,6101).tolist()
daa = pd.read_csv(filename,comment='C',header = None,sep = '\s+', names = column_name, parse_dates=[0],low_memory=False,index_col=False)
daa = daa.iloc[6:]
daa = daa.rename(columns = {"0":"Dates"})
答案 0 :(得分:2)
txt
文件的前6行date_var
date_var
row
是一个列表,并附加到数据中
''
中的空字符串row
。list(filter(None, row))
从列表中删除所有''
data
已加载到pandas
import csv
import pandas as pd
# forward fill the missing dates
data = list()
with open('FM2Sim_GW_HeadAllOut.txt', 'r') as f:
csv_reader = csv.reader(f, delimiter=' ')
date_var = ''
for i, row in enumerate(csv_reader):
if i > 5: # skip first 6 rows
if row[0]:
date_var = row[0]
# date_var = row[0][:10] # if you want to get rid of time, use this line
date_var = date_var.replace('24:00', '23:59') # remove this line, if removing time
row[0] = date_var
else:
row[0] = date_var
row = list(filter(None, row))
data.append(row)
# create the dataframe
df = pd.DataFrame(data)
# rename the date column
df.rename(columns={0: 'date'}, inplace=True)
# format as datetime
df.date = pd.to_datetime(df.date, format='%m/%d/%Y_%H:%M') # format='%m/%d/%Y' if time was removed
# save a new file
df.to_csv('new_file.csv', index=False)
print(df.iloc[:10, :10])
| | date | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---:|:--------------------|--------:|--------:|--------:|--------:|--------:|--------:|--------:|--------:|--------:|
| 0 | 1899-09-30 23:59:00 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 17.2917 | 17.2917 |
| 1 | 1899-09-30 23:59:00 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 17.2917 | 17.2917 |
| 2 | 1899-09-30 23:59:00 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 17.2917 | 17.2917 |
| 3 | 1899-09-30 23:59:00 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 14.3833 | 17.2917 | 17.2917 |
| 4 | 1899-10-31 23:59:00 | 13.5019 | 13.6842 | 14.0841 | 13.698 | 13.7531 | 13.9286 | 14.0963 | 15.9466 | 16.6629 |
| 5 | 1899-10-31 23:59:00 | 13.9378 | 14.0566 | 14.3744 | 14.0539 | 14.0964 | 14.1527 | 14.3827 | 15.4823 | 16.2371 |
| 6 | 1899-10-31 23:59:00 | 14.4266 | 14.5391 | 14.833 | 14.567 | 14.582 | 14.6196 | 14.9055 | 15.7093 | 16.4724 |
| 7 | 1899-10-31 23:59:00 | 14.8438 | 14.8858 | 15.1216 | 14.9813 | 14.9525 | 14.9419 | 15.1824 | 15.8385 | 16.5648 |
| 8 | 1899-11-30 23:59:00 | 13.0963 | 13.3783 | 13.9715 | 13.3591 | 13.444 | 13.7413 | 14.0693 | 15.3191 | 16.8376 |
| 9 | 1899-11-30 23:59:00 | 13.7826 | 13.9578 | 14.4 | 13.9429 | 13.9827 | 14.1416 | 14.4996 | 15.1693 | 16.3612 |