'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. 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.
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. I think I'm doing something wrong, because I can't figure out how to
- Get the number of cases for a single location in a single array
- Get the dates corresponding to each value in the cases array as a list (can't do array for this, right...?)
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...
(plus, would it be easier to do this with MATLAB? especially the plotting and everything?)
Solution 1:[1]
To get a list of the column headers, simply use df.columns
df = pd.DataFrame([(1, 2), (1, 3)], columns=['X', 'Y'])
df.columns # This outputs ['X', 'Y']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mohammad Ayoub |
