pandas

DatetimeIndex reverse-lookup memo as I’m not quite sure

Introduction I almost always feel confused about pandas.DatetimeIndex when I set DatetimeIndex in pandas. More, I tend to drown in the sea of superabundant information, so I decided to make personal notes here. Click the table of contents to jump to the relevant item! For more details, please check official pandas website. Data used for case studies and versions of pandas and Python This post uses the following DataFrame as variable name “df”. First of all, let’s make sure the data type of index is DatetimeIndex. print(df.index) DatetimeIndex(['2020-01-02 03:04:05', '2021-06-07 08:09:10','2022-11-12 13:14:15'], dtype='datetime64[ns]', name='YMDHMS', freq=None) Good good. Then versions of pandas and Python are 0.24.2 and 3.7.2 respectively. * ‘Extract “day of the week” as a string; day_name ()’ was curried out in pandas 1.0.5 (added on June 26, 2020) しときます。 Well then, prepare in advance; import pandas as pd import datetime Case studies Confirming DataFrame configuration; index, columns, values and name print(df.index) #confirm index DatetimeIndex(['2020-01-02 03:04:05', '2021-06-07 08:09:10', '2022-11-12 13:14:15'], dtype='datetime64[ns]', name='YMDHMS', freq=None) print(df.columns) #confirm columns Index(['a', 'b', 'c'], dtype='object')df.values print(df.index.name) #confirm index name 'YMDHMS' print(df.values) #confirm values array([['a0', 'b0', 'c0'], ['a1', 'b1', 'c1'], ['a2', 'b2', 'c2']], dtype=object) print(df.columns.name) # confirm column names None 指定した行番号のindex名を表示, index Print the index […]

Reading csv file with multiple delimiters in pandas

Introduction This is a memorandum about reading a csv file with read_csv of Python pandas with multiple delimiters. specifying the delimiter using sep (or delimiter) with stuffing these delimiters into “[]” So I’ll try it right away. Details Suppose I have the following csv file (tempo.csv) and I want to read it as separated with some delimiters (the right side of the time has a tab). s1,s2;s3,datetime       f1,f2,f3 a,b;c,2020/07/27 03:00  1.2,3.4,5.6 d,e;f,2021/09/28 13:03  2.3,4.5,6.7 g,h;i,2022/11/29 23:45  3.4,5.6,7.8 Here, let’s use the following seven types of delimiters to separate them. “,” “;” “/” ” ” (space) “:” “t”(tab) “.” How to specify the delimiter with sep (or delimiter) is just writing multiple delimiters in [] like this. sep = “[]” And specify engine =’python’ together. Done! By the way, if you read a file without specifying anything, the default delimiter will be “,” Therefore It will be like above. And more;reorder header You may already know by now… Reading a csv file as divided by multiple delimiters, the column header will be shifted and indexed weirdly…. So, replace the header with a list of column names according to the newly generated columns. All done! Reference site. Thank you.https://stackoverflow.com/questions/26551662/import-text-to-pandas-with-multiple-delimitershttps://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html?highlight=delimiter%20csv […]