Reputation: 971
I have a following sample list of lists:
In [] : list1
Out [] :
[[1.0],
[2.1],
[3.3, 5.5, 0.69],
[0.69, 0.9]]
I want to extract only the sublists where number of elements are equal and greater than 2 and want to store them in a data frame.
So, I expect a df something like below:
In [] : df
Out [] :
seq_no items
1 3.3 , 5.5, 0.69
2 0.69, 0.9
Tried:
item for item in list1 where(len(item) >2)
shows error.
Please let me know if anything is unclear.
Upvotes: 2
Views: 188
Reputation: 76917
In [755]: df = pd.DataFrame({'items': [x for x in list1 if len(x)>=2]})
In [756]: df
Out[756]:
items
0 [3.3, 5.5, 0.69]
1 [0.69, 0.9]
Add, seq_no
In [759]: df['seq_no'] = df.index + 1
In [760]: df
Out[760]:
items seq_no
0 [3.3, 5.5, 0.69] 1
1 [0.69, 0.9] 2
If you need a string of comma separated items
In [769]: pd.DataFrame({'items': [', '.join(map(str, x)) for x in list1 if len(x)>=2]})
Out[769]:
items
0 3.3, 5.5, 0.69
1 0.69, 0.9
Upvotes: 9
Reputation: 862511
Use list comprehension
and for seq
column range
:
a = [x for x in L if len(x) >=2]
df = pd.DataFrame({'seq':range(1, len(a)+1), 'items':a}, columns=['seq','items'])
print (df)
seq items
0 1 [3.3, 5.5, 0.69]
1 2 [0.69, 0.9]
Upvotes: 6
Reputation: 294218
You can store it in a pd.Series
first, then filter and transform.
s = pd.Series(list1)
pd.DataFrame(s[s.str.len().ge(2)].tolist())
0 1 2
0 0.00 0.50 0.69
1 0.69 0.88 1.00
2 1.00 1.10 NaN
3 1.10 2.00 NaN
4 2.00 2.50 2.90
And to join them
s = pd.Series(list1)
s[s.str.len().ge(2)].apply(lambda x: ', '.join(map(str, x)))
2 0.0, 0.5, 0.69
3 0.69, 0.88, 1.0
4 1.0, 1.1
8 1.1, 2.0
10 2.0, 2.5, 2.9
dtype: object
Upvotes: 5
Reputation: 2095
This should do it (with a list comprehension):
import pandas as pd
df = pd.Series([sublist for sublist in list1 if len(sublist) >= 2])
Then you can add
df.index += 1
to adjust the start index if you'd like.
Upvotes: 4