根据Pandas中的索引和条件将数据从列移动到行

ctrl_z

我的样本数据如下所示:

data = {'index':  ['001', '002', '003'],
        'A' : ['red', 'green', 'blue'],
        'B' : ['blue', 'yellow', 'green'],
        'C' : ['green', 'blue', 'red'],
        'A_new' : [2, 1, 3], 'B_new' : [0, 1, 2], 'C_new' : [0, 0, 1],
        'A_old' : [1, 0, 1], 'B_old' : [1, 0, 0], 'C_old' : [0, 0, 2],
        'A_other_new' : [2, 0, 1], 'A_other_old' : [1, 1, 0]}

df = pd.DataFrame (data, columns = ['index', 'A', 'B', 'C', 'A_new', 'B_new', 'C_new', 
                                'A_old', 'B_old', 'C_old', 'A_other_new', 'A_other_old']) 
df 

输出:

    index   A       B       C       A_new  B_new  C_new  A_old  B_old  C_old  A_other_new  A_other_old
0   001     red     blue    green   2      0      0      1      1      0      2            1
1   002     green   yellow  blue    1      1      0      0      0      0      0            1
2   003     blue    green   red     3      2      1      1      0      2      1            0
我正在尝试 'C' 根据索引和其他列将列从列 移到行,但是由于我只是一个初学者并且正在学习Pandas,所以很难。点是列之后移动成列的每个1值 'C' 到一个新的行,通过柱连接 'index' 'color' 'letter' 'reference' 到原始数据帧。在column下 'reference' ,如果原始数据列 'A_other_new' 'A_other_old' > 0, 'OX' 则每个+1值都需要有一个标记 (O是字母,X是数字)。这部分很重要,我无法跳过,因为我需要执行其他步骤。

有人对如何解决这个问题有想法吗?

我想要的输出是:

    index   color   letter   reference  age  
00  001     red      A       A1         new     
01  001     red      A       A2         new   
02  001     red      A       A3         old
03  001     red      A       O1         new
04  001     red      A       O2         new
05  001     red      A       O3         old 
06  001     blue     B       B1         old        
07  001     green    C       C1         0 
08  002     green    A       A1         new
09  002     green    A       O1         old  
10  002     yellow   B       B1         new
11  002     blue     C       C1         0 
12  003     blue     A       A1         new
13  003     blue     A       A2         new
14  003     blue     A       A3         new
15  003     blue     A       A4         old
16  003     blue     A       O1         new
17  003     green    B       B1         new
18  003     green    B       B2         new
19  003     red      C       C1         new
20  003     red      C       C2         old
21  003     red      C       C3         old
纽约市编码员

不确定我是否了解other您所需的输出中列,但是您可以melt在此处使用如果您能解释一下其他方式会更好:

df = df.melt(id_vars=['index', 'A', 'B', 'C'],
             var_name='other')
df['age'] = df['other'].str.split('_').str[1]
df['letter'] = df['other'].str.split('_').str[0]
print(df)

   index      A       B      C        other  value    age letter
0    001    red    blue  green        A_new      2    new      A
1    002  green  yellow   blue        A_new      1    new      A
2    003   blue   green    red        A_new      3    new      A
3    001    red    blue  green        B_new      0    new      B
4    002  green  yellow   blue        B_new      1    new      B
5    003   blue   green    red        B_new      2    new      B
6    001    red    blue  green        C_new      0    new      C
7    002  green  yellow   blue        C_new      0    new      C
8    003   blue   green    red        C_new      1    new      C
9    001    red    blue  green        A_old      1    old      A
10   002  green  yellow   blue        A_old      0    old      A
11   003   blue   green    red        A_old      1    old      A
12   001    red    blue  green        B_old      1    old      B
13   002  green  yellow   blue        B_old      0    old      B
14   003   blue   green    red        B_old      0    old      B
15   001    red    blue  green        C_old      0    old      C
16   002  green  yellow   blue        C_old      0    old      C
17   003   blue   green    red        C_old      2    old      C
18   001    red    blue  green  A_other_new      2  other      A
19   002  green  yellow   blue  A_other_new      0  other      A
20   003   blue   green    red  A_other_new      1  other      A
21   001    red    blue  green  A_other_old      1  other      A
22   002  green  yellow   blue  A_other_old      1  other      A
23   003   blue   green    red  A_other_old      0  other      A

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据条件将数据框的值移动到列表中

对文件中数据块内的行进行排序。根据列中的索引将一个块中的行移动到数据块中的不同位置

如何根据公共条目将数据库表行移动到列中

使用Pandas将数据从行移动到创建的列-Python

python pandas,转换数据集,将行移动到列

如何将重复的列数据移动到行中

将记录移动到 R 中的不同列和行

将数据从行移动到列 VBA

如何根据此 Pandas 数据框中的列条件创建新的索引行?

根据另一列将数据从行移动到列

将索引值移动到pandas数据框中的列名称中

如何将熊猫数据框多索引列移动到两行

根据行索引值从pandas数据框列中求和

根据groupby和TimeGrouper之后的列条件从pandas数据框中获取行

pandas:根据某些列和行中的条件划分数据框

根据条件将 Pandas 中的列转换为行

将 Pandas 数据帧行移动到最近的时间步

pandas - 将多个具有相同名称和不同缺失数据的列移动到单个列中,然后删除重复的列

如何重塑数据,将行移动到新列?

如何将数据从多列和多行移动到 excel 中的 1 个单行

Pandas Dataframe:将具有相同列值的多行移动到新的特定行中

试图根据条件将列中单元格的值移动到另一个

如何将光标移动到特定的行和列?

Dataframe将数据移动到随机列中?

根据条件将值从一列移动到另一列

根据其他数据行中的行和列信息索引数据帧

将重复的行移动到矩阵中的新列

将数据从行移动到R中的列,忽略缺失值并基于转置的列添加列

在pandas数据框中将索引从一列移动到另一列