I have the following dataframe:
A B C
A 1 3 0
B 3 2 5
C 0 5 4
All I want is shown below:
my_list = [('A','A',1),('A','B',3),('A','C',0),('B','B',2),('B','C',5),('C','C',4)]
Thanks in advance!
values. tolist() you can convert pandas DataFrame Column to List. df['Courses'] returns the DataFrame column as a Series and then use values. tolist() to convert the column values to list.
To access the names of a Pandas dataframe, we can the method columns(). For example, if our dataframe is called df we just type print(df. columns) to get all the columns of the Pandas dataframe.
The pandas. DataFrame. duplicated() method is used to find duplicate rows in a DataFrame. It returns a boolean series which identifies whether a row is duplicate or unique.
IIUC, you can do:
df.stack().reset_index().agg(tuple,1).tolist()
[('A', 'A', 1),
('A', 'B', 3),
('A', 'C', 0),
('B', 'A', 3),
('B', 'B', 2),
('B', 'C', 5),
('C', 'A', 0),
('C', 'B', 5),
('C', 'C', 4)]
You can stack
and use to_records
to obtain a record array from the result:
df.stack().to_frame().to_records().tolist()
# [('A', 'A', 1), ('A', 'B', 3), ('A', 'C', 0), ('B', 'A', 3),...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With