【问题标题】:selecting or matching specific columns by name lists[], or comma separate通过名称列表 [] 或逗号分隔选择或匹配特定列
【发布时间】:2019-06-15 10:30:17
【问题描述】:

我有一个目录;在这个目录里面有很多不同的扩展文件的子文件夹,所以我只想读取 xlsx 文件,然后我想通过指定名称来匹配列。喜欢使用列表 [] 或逗号分隔条件。然后我想从我指定的列中获取内容。有些文件只有一张,有些有两张。所有文件的列中的名称都相同。但内容不同。所以,我需要帮助让代码通过文件并仅搜索应在代码中指定的列名,然后将它们组合到一个 xlsx 文件 output.xlsx

import pandas as pd
import numpy as np
import os
import xlrd
from xlrd import open_workbook
import sys
import xlsxwriter as xlsw
#import shutil

dataframes = []

path = r'C:\Users\malotaibi\Documents\mofaq', for root, subdirs, files in os.walk(path):,for file in files:, if file.endswith(".xlsx"):,f = (os.path.join(root, file)),print(f),dataframes.append(f),all_data = pd.DataFrame (),for f in dataframes:,dataframes = pd.read_excel(f, sheet_name=None) ,print(dataframes),list = dataframes[['Id', 'Original id', 'Name', 'Logic/Query', 'Comments']], collection= [list],join = pd.concat(collection),join.to_excel("output.xlsx")

Traceback(最近一次调用最后一次):文件 “C:\Users\malotaibi\Desktop\dd.py”,第 39 行,在 list = dataframes[['Id', 'Original id', 'Name', 'Logic/Query', 'Comments']] TypeError: unhashable type: 'list'

【问题讨论】:

  • 所以你是一个班轮的粉丝?
  • 为什么你的一行里有很多?是语句拆分器吗?

标签: python


【解决方案1】:

您的代码可能是:

import pandas as pd
import numpy as np
import os
import xlrd
from xlrd import open_workbook
import sys
import xlsxwriter as xlsw
#import shutil

dataframes = []

path = r'C:\Users\malotaibi\Documents\mofaq'
for root, subdirs, files in os.walk(path):
    for file in files:
        if file.endswith(".xlsx"):
            f = (os.path.join(root, file))
            print(f)
            dataframes.append(f)
all_data = pd.DataFrame ()
for f in dataframes:
    dataframes = pd.read_excel(f, sheet_name=None)
    print(dataframes)
    list = dataframes[['Id', 'Original id', 'Name', 'Logic/Query', 'Comments']]
    collection= [list]
    join = pd.concat(collection)
    join.to_excel("output.xlsx")

试试这个:

import pandas as pd
import numpy as np
import os
import xlrd
from xlrd import open_workbook
import sys
import xlsxwriter as xlsw
#import shutil

dataframes = []

path = r'C:\Users\jainil\Documents'
for root, subdirs, files in os.walk(path):
    for file in files:
        if file.endswith(".xlsx"):
            f = (os.path.join(root, file))
            print(f)
            dataframes.append(f)
all_data = pd.DataFrame ()
for f in dataframes:
    dataframes = pd.read_excel(f, sheet_name=None)
    print(dataframes)
    list = dataframes['Sheet1']
    list=list['Id', 'Original id', 'Name', 'Logic/Query', 'Comments']
    collection= [list]
    join = pd.concat(collection)
    join.to_excel("output.xlsx")
    print(join)

所以你可以试试:

import pandas as pd
import numpy as np
import os
import xlrd
from xlrd import open_workbook
import sys
import xlsxwriter as xlsw
#import shutil

dataframes = []

path = r'C:\Users\jainil\Documents'
for root, subdirs, files in os.walk(path):
    for file in files:
        if file.endswith(".xlsx"):
            f = (os.path.join(root, file))
            print(f)
            dataframes.append(f)
all_data = pd.DataFrame ()
join=pd.DataFrame(columns=['Id', 'Original id', 'Name', 'Logic/Query', 'Comments'])
for f in dataframes:
    dataframes = pd.read_excel(f, sheet_name=None)
    print(dataframes)
    list = dataframes['Sheet1']
    list=list['Id', 'Original id', 'Name', 'Logic/Query', 'Comments']
    collection= [list]
    join = pd.concat(join,collection)


join.to_excel("output.xlsx")
print(join)

【讨论】:

  • 非常感谢您抽出宝贵时间,是的,您的组织正确
  • 我得到了这个结果。
  • 文件“pandas_libs\hashtable_class_helper.pxi”,第 1608 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: (Id', 'Original id', 'Name', 'Logic/Query' , '评论')
  • 请帮助解决这个问题。我需要匹配所有 xlsx 文件中相同名称的列,并将内容组合到输出中。
  • 这是 TypeError: unhashable type: 'list'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多