import pandas as pd
import os
import numpy as np
from geopy.distance import geodesic
# 设置存放文件的根目录
file_path = 'F:/配送车数据/Xny20191026/changanky第二次处理soc'# 修改路径
# 设置文件目录
filenames = os.path.dirname(file_path)# 待处理文件位置
print(filenames)
csv_dir = filenames + '/changanky第二次处理soc'# 修改路径
print(csv_dir)
# 批量读取文件名称
csv_file_paths = []
for root, dirs, files in os.walk(csv_dir):
# 当csv不为空时
if files:
for f in files:
if f.endswith('.csv'):
file_path = os.path.join(root, f)
#print(file_path)
#print(csv_file_paths)
csv_file_paths.append(file_path)
# 遍历文件名,批量导入数据
all_df = pd.DataFrame()
print(csv_file_paths)
for file in sorted(csv_file_paths):
df = pd.read_csv(file, header=0,encoding = 'utf-8',usecols=['计算速度'])
print(df)
df.to_csv('f:/suducsv.csv',encoding='gbk')
这样运行的结果为:
计算速度0 33.6303521 44.7463172 34.3558883 31.7418634 47.620775.. ...718 31.806655719 22.725981720 20.959260721 7.179751722 1.180625[723 rows x 1 columns] 计算速度0 27.2364281 25.7247162 28.5660273 31.8388744 29.502339... ...1641 1.0433671642 0.1713121643 1.4020691644 0.7252391645 3.244010[1646 rows x 1 columns] 计算速度0 0.5729731 0.6113072 0.7235153 0.0504344 0.039972... ...1157 53.4116871158 59.2485411159 60.3354181160 58.5385591161 62.900598
所有文件的值是分开的,我想见他合并,请问怎么操作?