Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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_Function_Dataframe_Lambda - Fatal编程技术网

Python 如何编写将数据帧的列转换为单个列的函数?

Python 如何编写将数据帧的列转换为单个列的函数?,python,pandas,function,dataframe,lambda,Python,Pandas,Function,Dataframe,Lambda,我有这样一个数据帧: A = ID Material1 Materia2 Material3 14 0 0 0 24 1 0 0 12 1 1 0 25 0 0 2 A = ID Materials 14 Nan 24 Material1 12 Materi

我有这样一个数据帧:

A =   ID Material1 Materia2 Material3 
      14   0        0        0
      24   1        0        0 
      12   1        1        0
      25   0        0        2
A =   ID  Materials
      14   Nan 
      24   Material1
      12   Material1 
      12   Material2
      25   Material3 
      25   Material3 
我希望在一列中包含所有信息,如下所示:

A =   ID Material1 Materia2 Material3 
      14   0        0        0
      24   1        0        0 
      12   1        1        0
      25   0        0        2
A =   ID  Materials
      14   Nan 
      24   Material1
      12   Material1 
      12   Material2
      25   Material3 
      25   Material3 
有人能帮我写一个函数吗

与按计数重复行一起使用和:

编辑:对于仅添加具有缺失值的
0
材料,使用左键按原始
df['ID']
在一列数据框中进行连接,不重复:


它工作得很好,但是如何保存没有材质的行,因为现在我只有至少一个材质的ID?我刚刚编辑了我的问题