简体   繁体   English

如何将pandas dataframe中的列标题转换为列表?

[英]How to convert the column headings in pandas dataframe to a list?

Okay, idk if this is a weird/simple question, but I'm confused about something.好吧,如果这是一个奇怪/简单的问题,我想知道,但我对某些事情感到困惑。 So I took this file, and I wanted to sort of extract the data for a specific country so that I could plot the daily total number of cases vs the dates that they were recorded on.所以我拿了这个文件,我想提取特定国家/地区的数据,这样我就可以 plot 每日病例总数与记录日期的对比。 Since it was a csv file, I thought of using pandas to get a dataframe, and then used.loc to get the row with the country that I was looking it.由于它是一个 csv 文件,我想到了使用 pandas 来获取 dataframe,然后使用 .loc 来获取我正在查找的国家/地区所在的行。

I now have a single row in my dataframe, with a LOT of columns, the latter of which all correspond to the dates at which the specific number of cases occurred.我现在在我的 dataframe 中只有一行,有很多列,其中的列都对应于发生特定数量案例的日期。 I think I'm doing something wrong, because I can't figure out how to我想我做错了什么,因为我不知道该怎么做

  1. Get the number of cases for a single location in a single array获取单个数组中单个位置的案例数
  2. Get the dates corresponding to each value in the cases array as a list (can't do array for this, right...?)获取与 cases 数组中每个值对应的日期作为列表(不能为此做数组,对吧......?)

Is there any way I can take the headings and convert them to a list so that I can get the dates?有什么办法可以获取标题并将它们转换为列表以便获取日期? I'll probably figure out how to get the cases data in an array anyway, but this is sort of stumping me, so any help/suggestion would be appreciated.无论如何,我可能会弄清楚如何在数组中获取案例数据,但这有点难倒我,所以任何帮助/建议都将不胜感激。 I'm not extremely skilled/experienced with python/programming in general, so I'm kind of lost...一般来说,我对 python/编程不是很熟练/经验丰富,所以我有点迷路了……

(plus, would it be easier to do this with MATLAB? especially the plotting and everything?) (另外,使用 MATLAB 会更容易吗?尤其是绘图和其他一切?)

To get a list of the column headers, simply use df.columns要获取列标题列表,只需使用df.columns

df = pd.DataFrame([(1, 2), (1, 3)], columns=['X', 'Y'])
df.columns # This outputs ['X', 'Y']

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

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