简体   繁体   English

如何读取列名pandas的不区分大小写的字符串

[英]How to read case-insensitive string of column name pandas

How can I read excel documents which have the same number of columns and it should have same names of columns, but in some columns could be uppercase "d" and in others lowercase "d"? 如何读取具有相同列数且应该具有相同列名的excel文档,但是在某些列中可以是大写的“ d”,而在另一些列中可以是小写的“ d”? I am appending data frames which I have to read first, but I have the problem when some columns have the name "Student_IDs" and others "Student_Ids" 我要附加必须首先阅读的数据帧,但是当某些列的名称为“ Student_IDs”而另一些列的名称为“ Student_Ids”时,我遇到了问题

for example: 例如:

df1 DF1

  A              Student_IDs
  some text      text
  text           some text

df2 DF2

  A              Student_Ids
  text1          some text1
  text2          some text2
  text3          some text3

this is the code (where dirname is the name of folder from wich I am reading excel documents): 这是代码(其中dirname是我正在阅读excel文档的文件夹的名称):

for f in glob.glob(dirname + "/*.xlsx"):
    dfMerged = pd.read_excel(f)
    all_data = all_data.append(dfMerged,ignore_index=True)

and I have three columns instead of two. 我有三列而不是两列。

EDIT: I need the names of columns to be "A" and "Student_IDs". 编辑:我需要列的名称为“ A”和“ Student_IDs”。

您可以通过对dfMerged.columns = [x.lower() for x in dfMerged.columns]进行dfMerged.columns = [x.lower() for x in dfMerged.columns]来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM