将虚拟变量值从1更改为列名,然后创建一个列表,我可以将其与

时间:2018-12-14 00:01:27

标签: python pandas list rename dummy-variable

我有一个看起来像这样的数据框:

A    B    C

1    0    0
1    1    0
0    1    0
0    0    1

我想用相应的列名替换所有值,以便数据看起来像:

 A    B    C  

 A    0    0  
 A    B    0   
 0    B    0   
 0    0    C   

然后,我要创建一个列,该列是所有列值的列表,如下所示:

 A    B    C         D

 A    0    0   ['A','0','0']
 A    B    0   ['A','B','0']
 0    B    0   ['0','B','0']
 0    0    C   ['0','0','C']

最后,我想按D列分组并计算每个模式的出现次数。

2 个答案:

答案 0 :(得分:2)

您可以使用mul

df.mul(df.columns).replace('',0)
Out[63]: 
   A  B  C
0  A  0  0
1  A  B  0
2  0  B  0
3  0  0  C
#df['D']=df.mul(df.columns).replace('',0).values.tolist()

答案 1 :(得分:1)

必须使用 cleaner 方法来实现,但是您可以使用:

5

输出:

 export const picture = (uri, mime = 'application/octet-stream') => {
  //const mime = 'image/jpg';
  const { currentUser } = firebase.auth();  
  const Blob = RNFetchBlob.polyfill.Blob;
  const fs = RNFetchBlob.fs;
  window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
  window.Blob = Blob;

  return ((resolve, reject) => {
      const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri;
      let uploadBlob = null;
      const imageRef = firebase.storage().ref('your_ref').child('child_ref');
      fs.readFile(uploadUri, 'base64')
      .then((data) => {
        return Blob.build(data, { type: `${mime};BASE64` });
      })
      .then((blob) => {
        uploadBlob = blob;
        imageRef.put(blob._ref, blob, { contentType: mime });
      })
      .then(() => {
        //take the downloadUrl in case you want to downlaod
        imageRef.getDownloadURL().then(url => {
         // do something
        });
      });
  });
};

PS:W-B's answer更清洁的方式。