Python,pandas循环仅在最后一项中有效
编辑:我的道歉:我发现了问题,我正在使用Rstudio运行代码,这会弄糟一些事情,我只是从控制台尝试一下,它工作正常 我知道,我在做一些愚蠢的事情,但我不知道我做错了什么,我构建了这个脚本,它读取一个zip文件,进行一些转换并编写最后一个csv,但出于某种原因,只编写了最后一个文件 脚本是完全可复制的,如果您想尝试并调试它,源文件位于下面的链接中Python,pandas循环仅在最后一项中有效,python,pandas,rstudio,Python,Pandas,Rstudio,编辑:我的道歉:我发现了问题,我正在使用Rstudio运行代码,这会弄糟一些事情,我只是从控制台尝试一下,它工作正常 我知道,我在做一些愚蠢的事情,但我不知道我做错了什么,我构建了这个脚本,它读取一个zip文件,进行一些转换并编写最后一个csv,但出于某种原因,只编写了最后一个文件 脚本是完全可复制的,如果您想尝试并调试它,源文件位于下面的链接中 files = os.listdir(os.curdir) files = [i for i in files if i.endswith('.zip
files = os.listdir(os.curdir)
files = [i for i in files if i.endswith('.zip')]
print(files)
for x in files:
path_file = os.path.join(curDir ,x)
print(path_file)
source = pd.read_csv(path_file,
skiprows=1,
usecols=["DISPATCH","1" ,"SETTLEMENTDATE", "RUNNO","INTERVENTION","CASESUBTYPE","SOLUTIONSTATUS","NONPHYSICALLOSSES"],
dtype=str)
source.rename(columns={'1': 'version'}, inplace=True)
source.query('version=="2"')
################ Extract UNIT, SETTLEMENTDATE,DUID,INITIALMW AND EXPORT TO CSV
df_unit=source
df_unit=df_unit.query('DISPATCH=="DUNIT" or DISPATCH=="TUNIT"')
#Make first row a header
df_unit.columns = df_unit.iloc[0]
df_unit = df_unit[1:]
#create a conditional column
df_unit.loc[df_unit['DUNIT'] == 'TUNIT', 'INITIALMW1'] = df_unit['INTERVENTION']
df_unit.loc[df_unit['DUNIT'] == 'DUNIT', 'INITIALMW1'] = df_unit['INITIALMW']
df_unit.drop(columns=['RUNNO','2','INTERVENTION','INITIALMW','DISPATCHMODE'],inplace=True)
df_unit.rename(columns={'INITIALMW1': 'INITIALMW','DUNIT': 'UNIT'}, inplace=True)
df_unit=df_unit.query('SETTLEMENTDATE!="SETTLEMENTDATE" and INITIALMW !="0"')
df_unit["INITIALMW"] = pd.to_numeric(df_unit["INITIALMW"])
df_unit['SETTLEMENTDATE']=pd.to_datetime(df_unit['SETTLEMENTDATE'])
df_unit.head()
df_unit.to_csv(x.rsplit('.', 1)[0] + '.csv',float_format="%.4f",
index=False,date_format='%Y-%m-%dT%H:%M:%S.%fZ',compression='gzip')
print(path_file)
编辑:我添加了列表文件:['PUBLIC_DAILY_201906040000_20190605040502.zip', “公共_日报_201906050000_20190606040501.zip”, “公共_每日_201907140000_20190715040502.zip”]
这些文件是从下载的
下面的代码适用于我。可能当前目录中只有一个下载的zip文件,或者实际上您正在从错误目录调用jupyter笔记本或python脚本。您可以打印
os.getcwd()
。否则,代码就没有问题。所有zip文件都必须位于同一目录中,通过python脚本或jupyter notebook可以从中运行此代码
files = os.listdir(os.getcwd())
files = [i for i in files if i.endswith('.zip')]
print(files)
for x in files:
path_file = os.path.join(os.getcwd() ,x)
print(path_file)
...
...
df_unit.to_csv(x.rsplit('.', 1)[0] + '.csv',float_format="%.4f",
index=False,date_format='%Y-%m-%dT%H:%M:%S.%fZ',compression='gzip')
print(path_file)
你能粘贴
文件吗?看起来怎么样?@vbrises请看edit@Mim:我认识到,在您调用到\u csv
时,您没有添加路径。是否有可能该文件实际上已写入另一个目录?您已经检查过由os.getcwd()
返回的目录了吗?如果您想将文件写入与输入文件相同的目录,您可能只需将x
替换为path\u-csv
行中的path\u-file