How can I make "index_col" from pandas.read_csv case insensitive when parsing dates from different CSV files?

huangapple go评论55阅读模式
英文:

How can I make "index_col" from pandas.read_csv case insensitive when parsing dates from different CSV files?

问题

我正在尝试解析不同 CSV 文件中的日期,然后将解析后的日期用作我的数据框的索引列,就像这样:

df = pd.read_csv(csv, parse_dates=True, index_col="date")

但我需要使 index_col 不区分大小写,因为某些 CSV 文件的列名是 "DATE",而其他一些则不是。

我尝试过自己查找,但在谷歌上找不到解决方法,如果可能的话,我真的需要一些帮助。

英文:

I am trying to parse dates from different csv files then use the parsed dates as my dataframe's index column like this:

df = pd.read_csv(csv, parse_dates=True, index_col="date")

But I need index_col to be case insensitive because some csv files have "DATE" and some don't

I've tried myself but can't figure out a way after googling and would really like some help if it's even possible

答案1

得分: 1

你可以首先读取CSV文件,然后将所有列都转换为小写(这样更容易输入)。

英文:

You could read the CSV first then make all the columns lowercase (which is easier to type anyway).

pd.read_csv(csv, parse_dates=True).rename(columns=str.lower).set_index('date')

huangapple
  • 本文由 发表于 2023年5月22日 07:58:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定