dataframe values.tolist() datatype

前端 未结 2 792
栀梦
栀梦 2021-01-01 07:27

I have a dataframe like this:

This dataframe has several columns. Two are of type float: price and change, while

2条回答
  •  旧巷少年郎
    2021-01-01 07:31

    I think the pandas documentation helps:

    DataFrame.values

    Numpy representation of NDFrame

    The dtype will be a lower-common-denominator dtype (implicit upcasting); that is to say if the dtypes (even of numeric types) are mixed, the one that accommodates all will be chosen. Use this with care if you are not dealing with the blocks.

    So here apparently float is chosen to accomodate all component types. A simple method would be (however, most possibly there are more elegant solutions around, I'm not too familiar with pandas):

    datatmp = map(lambda row: list(row[1:]), df.itertuples())
    

    Here the itertuples() gives an iterator with elements of the form (rownumber, colum1_entry, colum2_entry, ...). The map takes each such tuple and applies the lambda function, which removes the first component (rownumber), and returns a list containing the components of a single row. You can also remove the list() invocation if it's ok for you to work with a list of tuples.

    [Dataframe values property][1] "http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.values.html#pandas.DataFrame.values"

提交回复
热议问题