Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataFrame to list of list without change in data type of values

Tags:

python

pandas

df.values.to_list() or list(df.values) converts dataframe to list of lists, but the integer values are converted to float values

DataFrame is,

   HSCode  value  year
0       2   0.18  2018
1       3   0.00  2018
2       4  12.48  2018
3       6   0.00  2018
4       7   1.89  2018

output required is

[[2,0.18,2018],[3,0.00,2018]..]

But df.values.tolist() gives

[[2.0,0.18,2018.0],...]
like image 688
SUDHEER TALLURI Avatar asked Aug 30 '19 16:08

SUDHEER TALLURI


People also ask

How do you convert a DataFrame to a list?

At times, you may need to convert your pandas dataframe to List. To accomplish this task, ' tolist() ' function can be used.

What is Tolist () in pandas?

Pandas series can be converted to a list using tolist() or type casting method. There can be situations when you want to perform operations on a list instead of a pandas object. In such cases, you can store the DataFrame columns in a list and perform the required operations.

Can pandas series hold different data types?

Series is a one-dimensional labeled array capable of holding data of the type integer, string, float, python objects, etc.

How do I get a list of unique values from a column in pandas?

You can get unique values in column (multiple columns) from pandas DataFrame using unique() or Series. unique() functions. unique() from Series is used to get unique values from a single column and the other one is used to get from multiple columns.


2 Answers

itertuples

list(map(list, df.itertuples(index=False)))

[[2, 0.18, 2018],
 [3, 0.0, 2018],
 [4, 12.48, 2018],
 [6, 0.0, 2018],
 [7, 1.89, 2018]]

A little more terse

And far less readable

[*map(list, zip(*map(df.get, df)))]

[[2, 0.18, 2018],
 [3, 0.0, 2018],
 [4, 12.48, 2018],
 [6, 0.0, 2018],
 [7, 1.89, 2018]]
like image 93
piRSquared Avatar answered Sep 29 '22 08:09

piRSquared


You can use the intermediate numpy records array to conserve datatypes, and then if you must, convert to a list.

This approach, while being quite fast, will leave you with a list of tuples, as opposed to a list of lists.


df.to_records(index=False).tolist()

[(2, 0.18, 2018),
 (3, 0.0, 2018),
 (4, 12.48, 2018),
 (6, 0.0, 2018),
 (7, 1.89, 2018)]
like image 33
user3483203 Avatar answered Sep 29 '22 09:09

user3483203