String python正在将数据类型从str转换为列表类型
我正在从配置文件中读取一个bin列表,它被读取为str。 我想将str转换为list类型,以便在bin函数中使用 这里有一个例子String python正在将数据类型从str转换为列表类型,string,list,pandas,binning,String,List,Pandas,Binning,我正在从配置文件中读取一个bin列表,它被读取为str。 我想将str转换为list类型,以便在bin函数中使用 这里有一个例子 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
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)
我得到一个错误指示
ValueError('bins must increase monotonically.')
IndexError: list assignment index out of range
在help(pd.cut)
中,它似乎希望bin
是一个整数列表,而不是字符串:
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.
试试这个:
bins = [0,50,75,np.inf]
not
bins = str([0,50,75,np.inf])
但我是从一个配置文件中读取的。。如何直接从配置文件中读取序列或列表如何读取配置文件中的数据,这就是缺少的内容。我需要实际的数据。。这就是代码。好的,我想我知道发生了什么,这个文件有str,你需要一个列表,是正确的。一旦将confiq文件中的数据添加到问题中。。。我会再看一遍。