简体   繁体   English

数据框列中的一组值的长度相同

[英]Same lengtht for a group of values in a column of a Data Frame

I would like to get the same lengtht for a group of values (0,1,2,3,4,5) in a column of a Data Frame.我想为数据框的列中的一组值(0、1、2、3、4、5)获得相同的长度。 For example, I would like to reduce the amount of every group of values to the lowest exisiting (= 41)例如,我想将每组值的数量减少到现有的最低值(= 41)

input: train_df.relevancy.value_counts()输入: train_df.relevancy.value_counts()

output: output:

output output

IIUC use GroupBy.head by minimal value from Series.value_counts : IIUC 通过GroupBy.head的最小值使用Series.value_counts

val = train_df.relevancy.value_counts().min()
train_df = train_df.groupby('relevancy').head(val)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 数据框 - 对列值进行分组,然后随机化该列的新值 - Pandas data frame - Group a column values then Randomize new values of that column 将一组数据框行的列值转换为列中的列表 - Convert column values for a group of data frame rows into a list in the column 根据两列中的值条件对数据框进行分组和汇总 - Group-by and aggregate a data frame with conditions of values from two column 在熊猫数据框中按具有半冒号分隔值的列分组 - Group by a column with semi colon separated values in pandas data frame 用同一列中相邻行的平均值替换数据框中的零 - Replace zeros in the data frame with average values of adjacent rows in the same column 如果满足基于同一数据帧中其他2列的行值的条件,则在数据帧的列行中填充值 - Filling values in rows of column in a data frame, if condition based on 2 other columns row values in the same data frame is met Pandas 比较同一数据框中的列并根据比较替换值 - Pandas compare column in same data frame and replace values based on comparison 将一个数据帧中的零值列替换为另一个数据帧中的同名列的平均值 - Replace zero valued columns in one data frame with mean values of same name column in another data frame 如何有条件地根据同一数据帧另一列中的值对Pandas数据帧中的行进行计数? - How to count rows in a data frame in Pandas conditionally against values in another column of the same data frame? 如何使用'group by'和'cut'方法在pandas数据框中使用连续分布按一系列列值分组? - how to group by a range of column values using continuous distribution in pandas data frame using 'group by' and 'cut' method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM