【发布时间】:2018-07-07 04:26:08
【问题描述】:
下面写的是我昨天问的问题。
Reading multiple CSV files and joining them horizontally
我昨天写的代码工作得很好,我能够得到组合的 csv 文件,但是当我想对其他文件夹执行此操作时,通过更改路径名和文件名,变量和数据框没有被初始化。这是我写的代码
import pandas as pd
import numpy as np
from pandas.io.common import EmptyDataError
path = '~/Documents/EditedElectoral/Torpa_Full_Data/'
def merged_data(i):
try:
age = pd.read_csv(path+str(i)+'_trimmed.pdf-age.csv', header=None, error_bad_lines=False, delim_whitespace=True)
except EmptyDataError:
age = pd.DataFrame()
try:
gender = pd.read_csv(path+str(i)+'_trimmed.pdf-gender.csv', header=None, error_bad_lines=False, delim_whitespace=True)
except EmptyDataError:
gender = pd.DataFrame()
try:
rel = pd.read_csv(path+str(i)+'_trimmed.pdf-rel.csv', header=None, error_bad_lines=False, delim_whitespace=True)
except EmptyDataError:
rel = pd.DataFrame()
combined = pd.concat([age, gender, rel], axis=1)
combined['block'] = str(i)
combined.to_csv(path+str(i)+'-combined.csv', header=None, index=None)
for num in range(1,242):
merged_data(num)
第一个错误是 csv 没有被导出。当我尝试运行combined.info() 和age.info() 时,我得到了NameError: name 'combined' is not defined
文件运行正常,没有任何错误,但我没有得到输出。这是否意味着我犯了一个逻辑错误?我在这方面犯了什么错误?
【问题讨论】: