Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫用标签剪切多个列?_Python_Pandas - Fatal编程技术网

Python 熊猫用标签剪切多个列?

Python 熊猫用标签剪切多个列?,python,pandas,Python,Pandas,我想剪切并标记多列中的值。基本上: df[numericColumn] = pd.cut(df[numericColumn], 3, labels=["small", "medium", "big"]) 我找到了这段代码(它可以工作),但没有做我想要的标记,当我用标签替换bin时,它出错了,并说我需要包含一个bin df = pd.DataFrame(np.random.rand(10,4)) df.apply(pd.cut, bin

我想剪切并标记多列中的值。基本上:

df[numericColumn] = pd.cut(df[numericColumn], 3, labels=["small", "medium", "big"])
我找到了这段代码(它可以工作),但没有做我想要的标记,当我用标签替换bin时,它出错了,并说我需要包含一个bin

df = pd.DataFrame(np.random.rand(10,4))
df.apply(pd.cut, bins=[0,0.5,1])

有没有办法在pandas中剪切多个列的标签?

因此,将其稍微分开,以显示发生了什么。您将看到,您可以设置存储箱,然后逐行进行剪切,然后将剪切结果更改为要使用的类别名称

# set up a dataframe
df = pd.DataFrame(np.random.rand(10,4))
df.columns = ['col1','col2','col3','col4']

# set up the intervals
bins = pd.IntervalIndex.from_tuples([(0, 0.33), (0.33, 0.66), (0.66, 1)])

# do the cut on col1 for example
x = pd.cut(df["col1"].to_list(),bins)

# change the name of the catagories
x.categories = ['small','medium','large']
# put it back
df['col1'] = x
给予

如果您想遍历整个数据帧

# set up a dataframe
df = pd.DataFrame(np.random.rand(10,4))
df.columns = ['col1','col2','col3','col4']

# set up the intervals
bins = pd.IntervalIndex.from_tuples([(0, 0.33), (0.33, 0.66), (0.66, 1)])
names = ['small','medium','large']

for col in df.columns:
    x = pd.cut(df[col].to_list(),bins)
    x.categories = names
    df[col] = x

谢谢你,保罗。这真的很酷。我有100多列,可以批量完成吗?或者在每一列上迭代?我在函数中加入了通过列遍历数据帧的功能,您也可以编写一个函数并按列应用它。谢谢,对我来说似乎是合乎逻辑的。由于某种原因,当我运行这个时,我爸爸都以NaN的身份出现。我想这和我的数据有关,我想我已经弄明白了。我认为你的代码假设它已经是百分比了。我在数据上运行了一个最小-最大定标器,现在它似乎正在工作。好哇!我在想那怎么可能是错的。。。
# set up a dataframe
df = pd.DataFrame(np.random.rand(10,4))
df.columns = ['col1','col2','col3','col4']

# set up the intervals
bins = pd.IntervalIndex.from_tuples([(0, 0.33), (0.33, 0.66), (0.66, 1)])
names = ['small','medium','large']

for col in df.columns:
    x = pd.cut(df[col].to_list(),bins)
    x.categories = names
    df[col] = x