【问题标题】:Appending column values into new cell in the same row in Pandas dataframe将列值附加到 Pandas 数据框中同一行的新单元格中
【发布时间】:2020-09-04 14:01:11
【问题描述】:

我有一个 csv 文件,其中包含 namesub_asub_bsub_csub_dsegmentgender 列。我想创建一个新列classes,其中所有课程(sub-columns)用逗号分隔,每个学生都参加。

最简单的方法是什么?

结果数据框应如下所示:

+------+-------+-------+-------+-------+---------+--------+---------------------+
| name | sub_a | sub_b | sub_c | sub_d | segment | gender | classes             |
+------+-------+-------+-------+-------+---------+--------+---------------------+
| john | 1     | 1     | 0     | 1     | 1       | 0      | sub_a, sub_b, sub_d |
+------+-------+-------+-------+-------+---------+--------+---------------------+
| mike | 1     | 0     | 1     | 1     | 0       | 0      | sub_a, sub_c, sub_d |
+------+-------+-------+-------+-------+---------+--------+---------------------+
| mary | 1     | 1     | 0     | 1     | 1       | 1      | sub_a, sub_b, sub_d |
+------+-------+-------+-------+-------+---------+--------+---------------------+
| fred | 1     | 0     | 1     | 0     | 0       | 0      | sub_a, sub_c        |
+------+-------+-------+-------+-------+---------+--------+---------------------+

【问题讨论】:

  • 你能把那个excel作为文本发布在这里吗?一揽子复制并粘贴,然后将其格式化为代码块
  • @Datanovice 我编辑了我的问题。

标签: python pandas data-manipulation


【解决方案1】:

您可以将applyaxis=1 一起使用

例如:如果您的数据框喜欢

df
   A_a  A_b  B_b  B_c
0    1    0    0    1
1    0    1    0    1
2    1    0    1    0

你可以的

df['classes'] = df.apply(lambda x: ', '.join(df.columns[x==1]), axis = 1)
df
   A_a  A_b  B_b  B_c   classes
0    1    0    0    1  A_a, B_c
1    0    1    0    1  A_b, B_c
2    1    0    1    0  A_a, B_b

对于特定列上的apply,您可以先使用loc 进行过滤

#for your sample data
df_ = df.loc[:,'sub_a':'sub_d']             #or df.loc[:,'sub_a', 'sub_b', 'sub_c', 'sub_d']
df_.apply(lambda x: ', '.join(df_.columns[x==1]), axis = 1)

【讨论】:

  • 嘿@Dishin,如果我的其他列也有 0-1 值怎么办,我忘了指出。我的错。
【解决方案2】:

让我们试试dot

s=df.filter(like='sub')
df['classes']=s.astype(bool).dot(s.columns+',').str[:-1]

【讨论】:

  • 我忘了指出我的数据框的列具有 0-1 值,这很糟糕。我该怎么办?
  • @tacoisdelicious 在点之前做过滤
【解决方案3】:

您只能在 sub 列上使用 apply 来应用 lambda 函数,该函数将连接 sub 列的名称,其中列的值等于 1:

sub_cols = ['sub_a', 'sub_b', 'sub_c', 'sub_d']
df['classes'] = df[sub_cols].apply(lambda x: ', '.join(df[sub_cols].columns[x == 1]), axis=1)

【讨论】:

    【解决方案4】:

    您确实想要遍历行。但是,您不能直接将类添加到 DataFrame,因为 DataFrame 的所有列都需要同样长。所以诀窍是先生成列,然后再添加:

    subjects = ['subj_a', 'subj_b', 'subj_c']
    classes_per_student [] # the empty column
    
    for _, student in df.iterrows():
        # first create a list of the classes taken by this student
        classes = [subj for subj in subjects if student[subj]]
        # create a single string
        classes = ', '.join(classes)  
        # append to the column under construction
        classes_per_student.append(classes)
    
    # and finaly add the column to the DataFrame
    df['classes'] = classes_per_student
    
    

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      相关资源
      最近更新 更多