小编Sou*_*jee的帖子

将多个 CSV 文件合并到 Python 电子表格的单独选项卡中

我有一个代码,它在一个目录中生成多个 CSV 文件。我想在 excel 中生成一个报告,它将包含作为单独选项卡的 CSV 文件。我已经使用了以下代码:

import pandas as pd
import os
import csv
import glob    
path = "/MyScripts"
all_files = glob.glob(os.path.join(path, "*.csv"))
df_from_each_file = (pd.read_csv(f) for f in all_files)
df_from_each_file.to_excel(writer, sheet_name='ReturnData.csv')
writer.save()
Run Code Online (Sandbox Code Playgroud)

但它给出了以下错误: AttributeError: 'generator' object has no attribute 'to_excel' 不确定我哪里出错了。我需要导入任何特定的库来解决问题吗?

Python 版本是 2.7

python excel python-2.7 python-3.x pandas

8
推荐指数
2
解决办法
1万
查看次数

在 Python 中用空格替换 nan

以下是我的数据框:

Id,ReturnCreated,ReturnTime,TS_startTime
O108808972773560,Return Not Created,nan,2018-08-23 12:30:41
O100497888936380,Return Not Created,nan,2018-08-18 14:57:20
O109648374050370,Return Not Created,nan,2018-08-16 13:50:06
O112787613729150,Return Not Created,nan,2018-08-16 13:15:26
O110938305325240,Return Not Created,nan,2018-08-22 11:03:37
O110829757146060,Return Not Created,nan,2018-08-21 16:10:37
Run Code Online (Sandbox Code Playgroud)

我想用空白替换 nan。尝试了下面的代码,但它不起作用。

import pandas as pd
import numpy as np
df = pd.concat({k:pd.Series(v) for k, v in ordercreated.items()}).unstack().astype(str).sort_index()
df.columns = 'ReturnCreated  ReturnTime  TS_startTime'.split()
df1 = df.replace(np.nan,"", regex=True)
df1.to_csv('OrderCreationdetails.csv')
Run Code Online (Sandbox Code Playgroud)

请帮助我了解我哪里出错了,我该如何解决。

python numpy pandas

7
推荐指数
2
解决办法
6827
查看次数

标签 统计

pandas ×2

python ×2

excel ×1

numpy ×1

python-2.7 ×1

python-3.x ×1