Do dataframe columns need to have the same number of elements with a datetime index?

I know that pandas data frames can have NaN values. I mean specifically if I am creating a dataframe from a list of lists of various sizes. Do the columns that do not have as many rows as the longest column get filled with NaNs automatically? How would this be affected if I used a datetime index?

Add Comment
1 Answer(s)

You can try something like this

import pandas as pd  list1 = [1,2,3] list2 = ['ed1','ed2','ed3','ed4'] list3 = ['example'] list_date_index = ['2000-01-01', '1999-12-20', '2000-11-01', '1995-02-25', '1992-06-30']  df = pd.DataFrame.from_dict({'index':list_date_index,'list1': list1, 'list2': list2, 'list3': list3}, orient='index').T  df=df.set_index(datetime_index) df.drop('index',axis=1,inplace=True)  print(df2.index) df.head() 

output:

DatetimeIndex(['2000-01-01', '1999-12-20', '2000-11-01', '1995-02-25',                '1992-06-30'],               dtype='datetime64[ns]', freq=None)              list1   list2    list3 2000-01-01  1        ed1    example 1999-12-20  2        ed2    None 2000-11-01  3        ed3    None 1995-02-25  None     ed4    None 1992-06-30  None    None    None 

*edit: datetime index passing the datetime series

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.