用另一个相同的键控行中的非NA值填充键控行中的NA列值

时间:2019-08-08 20:38:55

标签: python pandas

给我的数据格式很奇怪

let findContent = (jsonArr, searchedItem) => {
   let arr = [];
   jsonArr.forEach(obj => {

  for(let content in obj) {
    if(content == 'text'){
      let filtered = obj[content].split(' ').filter(el => el == searchedItem);
      if(filtered){
        arr.push(obj[content]);
      }
    }
  }
return arr;
}

给出键“名称”,我想用其他第一个非NaN值基本上填充键第一行中的NaN值,并将其压缩为一行。

df = pd.DataFrame([[1, 2, None, None], [1, None, 4, None], [1, None, None, 9, None], [1, None, None, None, 4]])
df.columns = ['name', 'c1', 'c2', 'c3', 'c4']

  name  c1  c2  c3  c4
    1   2.0 NaN NaN NaN
    1   NaN 4.0 NaN NaN
    1   NaN NaN 9.0 NaN
    1   NaN NaN NaN 4.0
    2   1.0 NaN NaN NaN
    2   NaN 4.0 NaN NaN

完成此任务的最佳功能是什么?与first()一起使用groupby来获取第一个非NA值?

1 个答案:

答案 0 :(得分:0)

怎么样?

df = pd.DataFrame([[1, 2, None, None], [1, None, 4, None], [1, None, None, 9, None], [1, None, None, None, 4],[2, 1, None, None, None],[2, None, 4, None, None]])
df.columns = ['name', 'c1', 'c2', 'c3', 'c4']
df.bfill(inplace=True)
newdf = df.groupby('name').head(1)
newdf

name    c1      c2      c3      c4
1       2.0     4.0     9.0     4.0
2       1.0     4.0     NaN     NaN