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?
CodePudding user response:
Convert DataFrame to dictionaries by DataFrame.to_dict
and then create new DataFrame
:
df = pd.DataFrame(df.to_dict('list').items(), columns=['Key','Key'])
print (df)
Key Key
0 A [1, 3, 5]
1 B [2, 4, 6]
CodePudding user response:
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]