The data file is like given below. How shall I read through data frame?
'''
[[2020,1,22],0,0,0],
[[2020,1,23],0,0,0],
[[2020,1,24],0,0,0],
[[2020,1,25],0,0,0],
[[2020,1,26],0,0,0],
[[2020,1,27],0,0,0],
'''
The data file is like given below. How shall I read through data frame?
'''
[[2020,1,22],0,0,0],
[[2020,1,23],0,0,0],
[[2020,1,24],0,0,0],
[[2020,1,25],0,0,0],
[[2020,1,26],0,0,0],
[[2020,1,27],0,0,0],
'''
Read the data as a single column of strings:
df = pd.read_fwf('data.txt', header=None)
# or read as csv with sep='\n'
# df = pd.read_csv('data.txt', sep='\n', header=None)
Parse the list-looking strings into actual lists with ast.literal_eval and expand them into columns with apply(pd.Series):
from ast import literal_eval
df = df[0].str.strip(', ').apply(literal_eval).apply(pd.Series)
Convert the date lists to real datetimes:
df[0] = df[0].agg(lambda x: pd.to_datetime('-'.join(map(str, x))))
Output:
0 1 2 3
0 2020-01-22 0 0 0
1 2020-01-23 0 0 0
2 2020-01-24 0 0 0
3 2020-01-25 0 0 0
4 2020-01-26 0 0 0
5 2020-01-27 0 0 0
read_fwf is that it's for fixed-width formats, but there's no guarantee that the lines will all be the same width. For example, the month that is 1 on one line could be 11 on another, or the zeros in the 2nd through 4th columns could be replaced with any other numbers.colspecs or widths. the main caveat here is if there are spaces in the original data, in which case read_fwf will infer more columnsread_csv with sep='\n' also works if read_fwf gives you trouble