Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have a big list consisting of 31 nested dataframes. Each list dataframe has two strings: timestamp and measurement data. I want to convert them into individual dataframes.

biglist = [list1,list2,.........list31]

df1 = biglist[0]

.

.

df31 = biglist[30]

print(df1)

02/01/2019 09:30:23.000    26.77196

02/01/2019 10:10:32.000    59.66217

02/01/2019 10:50:32.000    85.40406

02/01/2019 11:30:32.000    61.95119

Now I am typing 31 lines to do it. Is there a simplest way of doing it? using for loop? etc.

I am using dictionary to call each dataframe. For iterating through dictionary, I am doing this

df=dict(enumerate(biglist1[1]))

    for i,x in df:

#        print(i,x)

        df[i] = df[i].apply(pd.to_numeric)

        df[i].index = pd.to_datetime(df[i].index)

It is working successfully with no errors. Now I want to combine data from 24 dataframes only. Code is

df[0]['time']=df[0].index  ### considering POA as reference dataframe

df[0]['time'] = df[0]['time'].apply(pd.to_datetime) # print(df[0].index) # print(dfm24.index) dfm1.dtypes

l=[]

s1 = []

for i,x in enumerate(df):           ## dfm24.duplicated(subset=None, keep='first')

    s1=df[0].reindex(df[i].index,method='nearest')

    b = 1

    if i>8:

        b = 2

    if i==1:

        l.append(s1.join(df[i]).set_index('time').add_suffix(df[i].columns[0][-b:]))

    if (i > 1) & (i<25):

        l.append(s1.join(df[i]).set_index('time').reindex(l[0].index,method='nearest').add_suffix(df[i].columns[0][-b:]))

combdf = pd.concat(l,1) # combdf.dtypes

This code is working successfully with no errors.

1 Answer

0 votes
by (25.1k points)

You can do it like this:

df=dict(enumerate(biglist1))

for i,x in df:

    print(i,x)

    df[i] = df[i].apply(pd.to_numeric)

    df[i].index = pd.to_datetime(df[i].index)

Browse Categories

...