简体   繁体   English

通过根据另一个数据帧上的条件选择pandas数据框列来创建numpy数组

[英]Creating a numpy array by selecting pandas data frame columns based on condition on another data frame

I am newbie to Pandas and I am trying hours to solve my problem but I haven't manage to solved it. 我是熊猫的新手,我正在努力解决我的问题,但我没有设法解决它。 I have seen several stackoverflow conversations but none it is similar to my problem. 我已经看过几个stackoverflow对话,但没有一个与我的问题类似。

I have two data frames, the training with shape (256, 800) and the labels (1, 800), each label correspond to one digit 0-9 and I want to create 10 numpy arrays where each contains only the training data of the i digit. 我有两个数据框,训练有形状(256,800)和标签(1,800),每个标签对应一个数字0-9我想创建10个numpy数组,其中每个只包含训练数据我喜欢。

If the first training example (1*256) has label 0 to assign the column (1*256) of the training to the first numpy array and so on. 如果第一个训练示例(1 * 256)具有标签0以将训练的列(1 * 256)分配给第一个numpy数组,依此类推。 ie iterating one dataframe assign to a numpy array based on condition to another dataframe. 即迭代一个数据帧根据条件分配给一个numpy数组到另一个数据帧。 If for example I have 89 "zeros" the 'zeros' numpy array will by 256*89.. 例如,如果我有89个“零”,则“零”numpy数组将为256 * 89。

Thanks in advance 提前致谢

This should work: dict is the dictionary and keys refer to the labels. 这应该工作:dict是字典,键是指标签。 I am assuming that labels is a one row multiple columns dataframe, that is why I transposed it. 我假设标签是一行多列数据帧,这就是我转置它的原因。

dict = {0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[], 8:[], 9:[]}
for i in range(df.shape[0]):
    dict[labels.T.[i]].append(df.iloc[i, :])

暂无
暂无

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

相关问题 如何根据pandas中另一个数据框中的条件更新数据框 - how to update a data frame based on the condition in another data frame in pandas 基于多列值的组合选择不同的熊猫数据框 - Selecting distinct pandas data frame based on combination of multiple columns value 如何根据条件在熊猫数据框的多列上分配值 - How to assign values on multiple columns of a pandas data frame based on condition 根据 pandas 数据框中另一列中的条件对一列求和 - Summing a column based on a condition in another column in a pandas data frame 从具有基于另一列的条件的 pandas 数据帧中删除重复项 - Removing duplicates from pandas data frame with condition based on another column 根据条件将值从一个pandas数据帧替换为另一个pandas数据帧 - Substitute values from one pandas data frame to another based on condition 基于另一个表更新熊猫数据框中的多列 - updating multiple columns in the pandas data frame based another table 在 Pandas 数据框中快速搜索并根据条件在数据框的另一列中插入值 - Fast search in pandas data frame and inserting values in another column of the data frame based on a condition 根据为pandas中另一个数据框中的列提供的条件对数据框的列执行操作 - perform operation on column of data frame based on condition given to column in another data frame in pandas 根据Condition修改Wide Pandas数据框 - Modifying Wide Pandas Data frame based on Condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM