我有一个这样的数据框,
df
col1 col2 col3
1907 CD 49
1907 FR 33
1907 SA 34
1908 PR 1
1908 SA 37
1909 PR 16
1909 SA 38
现在CD不具有col1 1908和1909值,FR不具有1908和1909值,PR不存在1907。
现在,我想创建具有col2值的行,而这些行的col3值不等于0的所有col1值。
所以最终的数据帧看起来像
df
col1 col2 col3
1907 CD 49
1907 FR 33
1907 SA 34
1907 PR 0
1908 CD 0
1908 FR 0
1908 PR 1
1908 SA 37
1908 CD 0
1908 FR 0
1909 PR 16
1909 SA 38
我可以使用带有每个可能col2值的for循环并与每个col1组进行比较来做到这一点。但是我正在寻找最有效的捷径。
答案 0 :(得分:2)
将DataFrame.unstack
与DataFrame.stack
一起用于由var isFetching: Bool = false
var offset = 0
var totalListOnServerCount = 20 // it must be returned from server
var pageSize = 10 // get 10 objects for instance
// MARK: - API Handler
private func fetchNotifications(){
// return from function if already fetching list
guard !isFetching else {return}
if offset == 0{
// empty list for first call i.e offset = 0
self.anyList.removeAll()
self.collectionView.reloadData()
}
isFetching = true
// API call to fetch notifications with given offset or page number depends on server logic just simple POST Call
APIHandler.shared.getNotifications(offset: offset) {[weak self] (response, error) in
if let response = response {
self?.isFetching = false
if self?.offset == 0{
// fetch response from server for first fetch
self?.notificationsResponse = response
if self?.refreshControl.isRefreshing ?? false {
self?.refreshControl.endRefreshing()
}
}else{
// append if already exist ( pagination )
self?.notificationsResponse?.notifications.append(contentsOf: response.notifications)
}
self?.collectionView.reloadData()
}
}
}
// MARK: - Collection View Delegate
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let anyList = responseFromServer else { return }
// check if scroll reach last index available and keep fetching till our model list has all entries from server
if indexPath.item == anyList.count - 1 && anyList.count < totalListOnServerCount{
offset += pageSize
fetchNotifications()
}
}
填充的所有组合:
0
另一个想法是将DataFrame.reindex
与MultiIndex.from_product
结合使用:
df = df.set_index(['col1','col2']).unstack(fill_value=0).stack().reset_index()
print (df)
col1 col2 col3
0 1907 CD 49
1 1907 FR 33
2 1907 PR 0
3 1907 SA 34
4 1908 CD 0
5 1908 FR 0
6 1908 PR 1
7 1908 SA 37
8 1909 CD 0
9 1909 FR 0
10 1909 PR 16
11 1909 SA 38
答案 1 :(得分:0)
我们还可以对DataFrame.stack
做DataFrame.pivot_table
:
df.pivot(*df).stack(dropna = False).fillna(0).rename('col3').reset_index()
或{{3}}
df.pivot_table(*df.iloc[:,::-1],fill_value = 0).unstack().rename('col3').reset_index()
输出
col1 col2 col3
0 1907 CD 49.0
1 1907 FR 33.0
2 1907 PR 0.0
3 1907 SA 34.0
4 1908 CD 0.0
5 1908 FR 0.0
6 1908 PR 1.0
7 1908 SA 37.0
8 1909 CD 0.0
9 1909 FR 0.0
10 1909 PR 16.0
11 1909 SA 38.0