[英]how open folder with multiple dataframes in python and merge into one csv file
07252018 = {name: "Carlos", age:"30", height: "15" }
name age height
Carlos 30 15
07262018 = {name: "Carlos", age:"30", height: "15" }
name age height
Carlos 30 15
and etc.. range of 700csv
03012018 = pd.read_csv("Data/03012018 )
03022018 = pd.read_csv("Data/03012018 )
03032018 = pd.read_csv("Data/03012018 )
03042018 = pd.read_csv("Data/03012018 )
03052018 = pd.read_csv("Data/03012018 )
and etc..
file = pd.cancat([03012018,03022018,03032018,03042018,03052018 ])
file.to_csv("Data/file")
IIUC, this should do: IIUC,这应该这样做:
Less efficient, more readable:效率较低,可读性更好:
def get_df():
df=pd.DataFrame()
for file in os.listdir():
if file.endswith('.csv'):
aux=pd.read_csv(file)
df=df.append(aux)
return df
And then:接着:
df=get_df()
More memory efficient, less readable: memory 效率更高,可读性更低:
def df_generator():
for file in os.listdir():
if file.endswith('.csv'):
aux=pd.read_csv(file)
yield aux
And then:接着:
generator=df_generator()
df = pd.DataFrame()
for table in generator:
df = df.append(table)
Note: for this to work as is, the script has to be INSIDE the folder with the csv's.注意:要使其按原样工作,脚本必须位于包含 csv 的文件夹内。 Else, you'll need to add the relative path to that folder from the folder your script will be in.
否则,您需要从脚本所在的文件夹中添加该文件夹的相对路径。
Example: If your script is in the folder "Project" and inside that folder you have the folder "Tables" with all your csv's, you do:示例:如果您的脚本位于文件夹“Project”中,并且在该文件夹中,您的文件夹“Tables”中包含所有 csv,您可以:
os.listdir('Tables/')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.