I have a dataframe imported with pandas from excel with a format like this:
df = pd.read_excel('excel_file.xlsx')
data = pd.DataFrame(df, columns=['A', 'B', 'C', 'D', 'E'])
A B C D E
12 bob 32 abc 123
12 jan 34 fbc 23
14 jan 32 ac 133
12 cat 32 abc 123
I would like to group them by the column B so the output would come as:
list[0] = [[12 bob 32 abc 123]]
list[1] = [[12 jan 34 fbc 23][14 jan 32 ac 133]]
list[2] = [[12 cat 32 abc 123]]
I've tried using duplicated function with no success
Thank you very much!!
You can do:
lst = [d.values.tolist() for (k,d) in df.groupby('B', sort=False)]
# check
for i in range(len(lst)): print(lst[i])
Output:
[[12, 'bob', 32, 'abc', 123]]
[[12, 'jan', 34, 'fbc', 23], [14, 'jan', 32, 'ac', 133]]
[[12, 'cat', 32, 'abc', 123]]