Dataframe Iterate items in one column and store it into array

Question:

I have a dataframe shown in below.

A B
1 2
3 4
5 6

And I want to make it like this:

Key Value
A {1, 3, 5}
B {2, 4, 6}

I can use .T to rotate the table and know .columns to set table header, but i don’t know how to combine items in a column into an array.

Besides, is it possible the array in deltalake?

Asked By: jasondesu

||

Answers:

Convert DataFrame to dictionaries by DataFrame.to_dict and then create new DataFrame:

df = pd.DataFrame(df.to_dict('list').items(), columns=['Key','Value'])
print (df)
  Key      Value
0   A  [1, 3, 5]
1   B  [2, 4, 6]
Answered By: jezrael

Yes, it is possible to combine the items in a column into an array in a Pandas DataFrame. Here is one way to do it:

import pandas as pd

create the dataframe

df = pd.DataFrame({'A': [1, 3, 5], 'B': [2, 4, 6]})

use the groupby and apply methods to combine the items in each column into an array

df = df.groupby(df.columns, axis=1).apply(lambda x: x.values.tolist())

rename the columns of the dataframe

df.columns = ['Key', 'Value']

print the result

>>> print(df)
    Key       Value
0   A  [1, 3, 5]
1   B  [2, 4, 6]
Answered By: Jakob Guldberg Aaes
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.