gpt4 book ai didi

python - 使用python将excel转换为 Feather 格式

转载 作者:行者123 更新时间:2023-12-04 20:25:20 25 4
gpt4 key购买 nike

我有一个(每天增长的)大约 100 个大 excel 文件的列表,我用 Python 对其进行分析。由于我必须对所有文件运行几个循环,我的分析变得越来越慢。因此,我想将所有 excel 文件转换为 Feather 格式(比如每周一次)。有没有聪明的方法来做到这一点?到目前为止我已经尝试过:

path = r"filepath\*_name*.xlsx"
file_list = glob.glob(path)
for f in file_list:
df = pd.read_excel(f, encoding='utf-8')
df[['boola', 'boolb']] = dfa[['boola', 'boolb']].astype(int)
pathname = f[:-5] + ".ftr"
df.to_feather(pathname)

但我收到以下错误消息:
ArrowInvalid: ('Could not convert stringa with type str: tried to convert to boolean', "Conversion failed for column stringb with type object")

最佳答案

这是解决我的问题的方法:

path = r"pathname\*_somename*.xlsx"
file_list = glob.glob(path)
for f in file_list:
df = pd.read_excel(f, encoding='utf-8', decimal=',', thousands='.')
for col in df.columns:
w= (df[[col]].applymap(type) != df[[col]].iloc[0].apply(type)).any(axis=1)
if len(df[w]) > 0:

df[col] = df[col].astype(str)

if df[col].dtype == list:
df[col] = df[col].astype(str)
pathname = f[:-4] + "ftr"
df.to_feather(pathname)
df.head()
, decimal=',', thousands='.'部分是必要的,因为我的输入文件采用欧洲标准格式化,即使用逗号作为小数分隔符和点作为千位分隔符

关于python - 使用python将excel转换为 Feather 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61638115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com