python pandas converting datatype from str to list type

时间:2016-07-11 22:21:15

标签: string list pandas binning

I'm reading a bin list from a config file and it is being read as str. I want to convert str to list type so that i can use in the bin function

Here is an example

import numpy as np
import pandas as pd
raw_data = {'student':['A','B','C'],'marks_maths':[75,90,99]}
df = pd.DataFrame(raw_data, columns = ['student','marks_maths'])
bins = str([0,50,75,np.inf])
groups = ['L','M','H']
df['maths_level'] = pd.cut(df['marks_maths'], bins, labels=groups)

I get an error indicating

ValueError('bins must increase monotonically.')
IndexError: list assignment index out of range

2 个答案:

答案 0 :(得分:1)

From help(pd.cut), it looks like it's expecting bins to be a list of integers, not a string:

cut(x, bins, right=True, labels=None, retbins=False, precision=3, include_lowest=False)
    Return indices of half-open bins to which each value of `x` belongs.
    Parameters
    ----------
    x : array-like
        Input array to be binned. It has to be 1-dimensional.
    bins : int or sequence of scalars
        If `bins` is an int, it defines the number of equal-width bins in the
        range of `x`. However, in this case, the range of `x` is extended
        by .1% on each side to include the min or max values of `x`. If
        `bins` is a sequence it defines the bin edges allowing for
        non-uniform bin width. No extension of the range of `x` is done in
        this case.

答案 1 :(得分:1)

try this:

bins = [0,50,75,np.inf]  
 not 
bins = str([0,50,75,np.inf])