python-代码技巧
- 简单tips
- 1、多个列表循环遍历
- 2、使用map对列表中多个文件进行批量操作
仅个人笔记使用,感谢点赞关注
简单tips
1、多个列表循环遍历
dataTypes = ["train","test"]
dataStations = ["1","2","3","4"]
parameters = [(datatype,station) for datatype in dataTypes for station in dataStations]
print(parameters )
#[('train', '1'), ('train', '2'), ('train', '3'), ('train', '4'), ('test', '1'), ('test', '2'), ('test', '3'), ('test', '4')]
2、使用map对列表中多个文件进行批量操作
train_files = glob.glob('train/*.csv')
test_files = glob.glob('test/*.csv')
print(test_files)test_files = pd.Series(test_files)
train_files = pd.Series(train_files)def read(file):d = pd.read_csv(file)#记录气象站编号d['index'] = int(file[-5])return d#将十个电站数据合并
test = pd.concat(test_files.map(read).values, axis = 0)
train = pd.concat(train_files.map(read).values, axis = 0)