Anna
Anna

Reputation: 974

How to iterate through a list of lists in Python and add values from other lists?


I have a list:

my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]

And try to iterate through it in order to get the values and its' positions, like so:

date    1
country 1
date    2
country 1
date    2

And store it all in a pandas DF.

As it was suggested I can do it like that, and it works perfectly:

Use list comprehension with enumerate and flattening for list of tuples:

my_list = [['ga:date'], ['ga:country', 'ga:date'], ['ga:country', 'ga:date']]

x = [(b, a) for i in my_list for (a, b) in enumerate(i, 1)]
print (x)
[('ga:date', 1), ('ga:country', 1), ('ga:date', 2), ('ga:country', 1), ('ga:date', 2)]

df = pd.DataFrame(x, columns = ['field','listIndex'])
print (df)
        field  listIndex
0     ga:date          1
1  ga:country          1
2     ga:date          2
3  ga:country          1
4     ga:date          2

Or if possible change position of columns:

x1 = [z for i in my_list for z in enumerate(i, 1)]
print (x1)
[(1, 'ga:date'), (1, 'ga:country'), (2, 'ga:date'), (1, 'ga:country'), (2, 'ga:date')]

df = pd.DataFrame(x1, columns = ['listIndex','field'])
print (df)
   listIndex       field
0          1     ga:date
1          1  ga:country
2          2     ga:date
3          1  ga:country
4          2     ga:date

But there are also 3 other lists, which I have to add to the resulted df.

my_id_list = ['01', '02', '03']

start_dates = ['2019-01-01', '2019-01-03', '2019-01-10']

end_dates = ['2019-01-02', '2019-01-05', '2019-01-11']

So it needs to look like that:

        field  listIndex   id start_date end_date
0     ga:date          1   01 2019-01-01 2019-01-02
1  ga:country          1   02 2019-01-03 2019-01-03
2     ga:date          2   02 2019-01-03 2019-01-03
3  ga:country          1   03 2019-01-10 2019-01-11
4     ga:date          2   03 2019-01-10 2019-01-11

Values can be different, there is no fix.

Would appreciate any help, I just want to end a project at work and forget it.

update

My id list contains of different int numbers. And they can differ, I mean, these 3 below are not the only ones.

my_id_list = ['115126931', '199714437', '197531387']

So it needs to look like that:

        field  listIndex   id        start_ date  end_date
0     ga:date          1   115126931 2019-01-01   2019-01-02
1  ga:country          1   199714437 2019-01-03   2019-01-03
2     ga:date          2   199714437 2019-01-03   2019-01-03
3  ga:country          1   197531387 2019-01-10   2019-01-11
4     ga:date          2   197531387 2019-01-10   2019-01-11

Upvotes: 1

Views: 310

Answers (1)

anky
anky

Reputation: 75080

You can try:

df=pd.DataFrame([(a,b,e) for e,i in enumerate(my_list) for (a, b) in enumerate(i, 1)],
                       columns=['list_index','feild','index_list_of_list'])
df1=pd.DataFrame(zip(map(int,my_id_list),start_dates,end_dates)
             ,columns=['id','startdate','enddate'])

df.merge(df1,left_on='index_list_of_list',right_index=True).drop('index_list_of_list',1)

   list_index       feild         id   startdate     enddate
0           1     ga:date  115126931  2019-01-01  2019-01-02
1           1  ga:country  199714437  2019-01-03  2019-01-05
2           2     ga:date  199714437  2019-01-03  2019-01-05
3           1  ga:country  197531387  2019-01-10  2019-01-11
4           2     ga:date  197531387  2019-01-10  2019-01-11

Note: also consider changing the dates to datetime by pd.to_datetime()

Upvotes: 1

Related Questions