'How to read a file into a dataframe where the values are grouped in parentheses?
I need to import a text file that is formatted as such
(201803, 95), (201708, 61)
(202002, 88), (201501, 88)
(202004, 76), (201602, 35)
(201012, 113), (201012, 115)
(201009, 114), (201708, 114)
(201603, 31), (201206, 78)
And turn it into a dataframe with this format where the first column is a date and the second column is an integer for temperature.
Year Temp
201803 95
201708 61
202002 88
201501 88
202004 76
201602 35
201012 113
201012 115
201009 114
201708 114
201603 31
201206 78
I tried to import it using pandas.read_csv but I get a dataframe with 4 columns and 2 rows of data in a row.
0 1 2 3
0 (201803 95) (201708 61)
1 (202002 88) (201501 88
Solution 1:[1]
Pandas imports exactly how it sees. To group things how you want in your case, you'll need to wrap the columns in the same set of quotes or simply restructure the CSV to be linearly stacked as you want for the output.
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 | Daniel S. |
