我想在找到Income
时添加否定D
的值
# Preparing data
# Note that I have clean up the `jan` to `Jan` and `Charge` to `charge`
[In]
data = [{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Income': 200},
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Income': 10},
{'Month': 'Feb', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'd', 'Ex/Im': 'export', 'Income': 100},
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Income': 400},
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Income': 10},
{'Month': 'Feb', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'd', 'Ex/Im': 'export', 'Income': 200}]
[Out] Months Name id Info d/k Ex/Im Income
0 Jan Alice sal 212 charge k export 200
1 Jan Alice sal 212 charge k export 10
2 Jan Alice sal 212 charge d export 100
3 Jan Alice sal 212 charge k Import 400
4 Jan Alice sal 212 charge k Import 10
5 Jan Alice sal 212 charge d Import 200
df = pd.DataFrame(data)
*注意: 我数据框中的名字不仅是爱丽丝·萨尔(alice sal),每个名字中我都有类似的情况
我已经尝试过
# 1. Negate the values of `Income` when `D` is found,
[In]:
df.loc[df['d/k'] == 'D', 'Income'] = -df[df['d/k'] == 'D']['Income']
print(df)
[Out]
Months Name id Info d/k Ex/Im Income
0 Jan Alice sal 212 charge k export 200
1 Jan Alice sal 212 charge k export
2 Jan Alice sal 567 charge d export
3 Jan Alice sal 212 charge k Import 400
4 Jan Alice sal 212 charge k Import
5 Jan Alice sal 567 charge d Import
# 2. Group by
[In]:
gdf = df.groupby(['id', 'Month', 'Ex/Im', 'Info']).agg({'Income':'sum', 'Name':'first'})
print(gdf)
[Out]:
Income
Month Jan
Ex/Im export import
Info charge charge
id Name
212 Alice sal 200 400
其他人的错误警告:
FutureWarning:逐元素比较失败;而是返回标量,但将来将执行元素比较 结果=方法(y)
我的期望:
Income
Month Jan
Ex/Im export import
Info charge charge
id Name
212 Alice sal 120 210
答案 0 :(得分:1)
使用condition更改值之后,这只是一个关键问题
df.loc[df['d/k']=='d','Income']=df.Income*-1
df.pivot_table(index=['id','Name'],columns=['Months','Ex/Im','Info'],values='Income',aggfunc='sum')
Out[423]:
Months Jan
Ex/Im Import export
Info charge charge
id Name
212 Alicesal 210 110
答案 1 :(得分:0)
除您的数据和取反部分外,一切都正确:
data = [
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Ex/Im': 'export', 'Income': 200},
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Ex/Im': 'export', 'Income': 10},
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'd', 'Ex/Im': 'export', 'Income': 100},
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Ex/Im': 'Import', 'Income': 400},
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'k', 'Ex/Im': 'Import', 'Income': 10},
{'Month': 'Jan', 'Name': 'Alice sal', 'id': 212, 'Info': 'charge', 'd/k': 'd', 'Ex/Im': 'Import', 'Income': 200}]
month
作为Feb
,但表视图中的所有记录均为Jan
。Ex/Im
列D
进行比较,因此您应将D
更改为d
,或将d/k
列中的值更改为D