根据行值获取值和列名称(单元格中的多个值)
我有这个 df
df = pd.DataFrame( {'R': {0: '1', 1: '2', 2: '3', 3: '4', 4: '5', 5: '6', 6: '7'},\
'a': {0: 1.0, 1: 1.0, 2: 2.0, 3: 3.0, 4: 3.0, 5: 2.0, 6: 3.0},\
'b': {0: 1.0, 1: 1.0, 2: 1.0, 3: 2.0, 4: 2.0, 5: 0.0, 6: 3.0},\
'c': {0: 1.0, 1: 2.0, 2: 2.0, 3: 2.0, 4: 2.0, 5: -2.0, 6: -2.0}, \
'd': {0: 1.0, 1: 2.0, 2: 1.0, 3: 0.0, 4: 1.0, 5: 2.0, 6: -1.0},\
'e': {0: 1.0, 1: 2.0, 2: 2.0, 3: 1.0, 4: 1.0, 5: 2.0, 6: -2.0}, \
'f': {0: -1.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: -2.0, 5: -1.0, 6: 2.0},\
'g': {0: 1.0, 1: 1.0, 2: 2.0, 3: 1.5, 4: 2.0, 5: 0.0, 6: 2.0}, \
'h': {0: 0.0, 1: 0.0, 2: 1.0, 3: 2.0, 4: 2.0, 5: 1.0, 6: 3.0}, \
'i': {0: 0.0, 1: -1.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: -3.0, 6: 3.0}, \
'j': {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 2.0, 5: -1.0, 6: -1.0}, \
'k': {0: 62, 1: 166, 2: 139, 3: 60, 4: 93, 5: 17, 6: 5}} )
,它给了我们
R a b c d e f g h i j k
0 1 1.0 1.0 1.0 1.0 1.0 -1.0 1.0 0.0 0.0 1.0 62
1 2 1.0 1.0 2.0 2.0 2.0 0.0 1.0 0.0 -1.0 1.0 166
2 3 2.0 1.0 2.0 1.0 2.0 0.0 2.0 1.0 0.0 1.0 139
3 4 3.0 2.0 2.0 0.0 1.0 0.0 1.5 2.0 0.0 1.0 60
4 5 3.0 2.0 2.0 1.0 1.0 -2.0 2.0 2.0 0.0 2.0 93
5 6 2.0 0.0 -2.0 2.0 2.0 -1.0 0.0 1.0 -3.0 -1.0 17
6 7 3.0 3.0 -2.0 -1.0 -2.0 2.0 2.0 3.0 3.0 -1.0 5
我需要 2 个新列
df['an']= 显示当前原始数据具有负值的每列的列名称
df['nv']= 显示当前原始数据具有负值的每列的负值
所需的输出
R a b c d e f g h i j k an nv
0 1 1.0 1.0 1.0 1.0 1.0 -1.0 1.0 0.0 0.0 1.0 62 'f' -1
1 2 1.0 1.0 2.0 2.0 2.0 0.0 1.0 0.0 -1.0 1.0 166 'i' -1
2 3 2.0 1.0 2.0 1.0 2.0 0.0 2.0 1.0 0.0 1.0 139 '-' -
3 4 3.0 2.0 2.0 0.0 1.0 0.0 1.5 2.0 0.0 1.0 60 '-' -
4 5 3.0 2.0 2.0 1.0 1.0 -2.0 2.0 2.0 0.0 2.0 93 'f' -2
5 6 2.0 0.0 -2.0 2.0 2.0 -1.0 0.0 1.0 -3.0 -1.0 17 'c,f,i,j' [-2,-1,-3,-1]
6 7 3.0 3.0 -2.0 -1.0 -2.0 2.0 2.0 3.0 3.0 -1.0 5 'c,d,e,j' [-2,-1,-2,-1]
我尝试了多个代码选项,例如 np.where 或 np.select,但我无法使其工作。
任何帮助将不胜感激。
I have this df
df = pd.DataFrame( {'R': {0: '1', 1: '2', 2: '3', 3: '4', 4: '5', 5: '6', 6: '7'},\
'a': {0: 1.0, 1: 1.0, 2: 2.0, 3: 3.0, 4: 3.0, 5: 2.0, 6: 3.0},\
'b': {0: 1.0, 1: 1.0, 2: 1.0, 3: 2.0, 4: 2.0, 5: 0.0, 6: 3.0},\
'c': {0: 1.0, 1: 2.0, 2: 2.0, 3: 2.0, 4: 2.0, 5: -2.0, 6: -2.0}, \
'd': {0: 1.0, 1: 2.0, 2: 1.0, 3: 0.0, 4: 1.0, 5: 2.0, 6: -1.0},\
'e': {0: 1.0, 1: 2.0, 2: 2.0, 3: 1.0, 4: 1.0, 5: 2.0, 6: -2.0}, \
'f': {0: -1.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: -2.0, 5: -1.0, 6: 2.0},\
'g': {0: 1.0, 1: 1.0, 2: 2.0, 3: 1.5, 4: 2.0, 5: 0.0, 6: 2.0}, \
'h': {0: 0.0, 1: 0.0, 2: 1.0, 3: 2.0, 4: 2.0, 5: 1.0, 6: 3.0}, \
'i': {0: 0.0, 1: -1.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: -3.0, 6: 3.0}, \
'j': {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 2.0, 5: -1.0, 6: -1.0}, \
'k': {0: 62, 1: 166, 2: 139, 3: 60, 4: 93, 5: 17, 6: 5}} )
which gives us
R a b c d e f g h i j k
0 1 1.0 1.0 1.0 1.0 1.0 -1.0 1.0 0.0 0.0 1.0 62
1 2 1.0 1.0 2.0 2.0 2.0 0.0 1.0 0.0 -1.0 1.0 166
2 3 2.0 1.0 2.0 1.0 2.0 0.0 2.0 1.0 0.0 1.0 139
3 4 3.0 2.0 2.0 0.0 1.0 0.0 1.5 2.0 0.0 1.0 60
4 5 3.0 2.0 2.0 1.0 1.0 -2.0 2.0 2.0 0.0 2.0 93
5 6 2.0 0.0 -2.0 2.0 2.0 -1.0 0.0 1.0 -3.0 -1.0 17
6 7 3.0 3.0 -2.0 -1.0 -2.0 2.0 2.0 3.0 3.0 -1.0 5
I need 2 new columns
df['an']= displays column name of each column where the current raw had negative value
df['nv']= displays negative values of each column where the current raw had negative value
Desired output
R a b c d e f g h i j k an nv
0 1 1.0 1.0 1.0 1.0 1.0 -1.0 1.0 0.0 0.0 1.0 62 'f' -1
1 2 1.0 1.0 2.0 2.0 2.0 0.0 1.0 0.0 -1.0 1.0 166 'i' -1
2 3 2.0 1.0 2.0 1.0 2.0 0.0 2.0 1.0 0.0 1.0 139 '-' -
3 4 3.0 2.0 2.0 0.0 1.0 0.0 1.5 2.0 0.0 1.0 60 '-' -
4 5 3.0 2.0 2.0 1.0 1.0 -2.0 2.0 2.0 0.0 2.0 93 'f' -2
5 6 2.0 0.0 -2.0 2.0 2.0 -1.0 0.0 1.0 -3.0 -1.0 17 'c,f,i,j' [-2,-1,-3,-1]
6 7 3.0 3.0 -2.0 -1.0 -2.0 2.0 2.0 3.0 3.0 -1.0 5 'c,d,e,j' [-2,-1,-2,-1]
I tried multiple code options, such as np.where or np.select, but I could not mmake it work.
Any help will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以对每行使用比较和布尔索引,使用赋值表达式保存中间变量,并创建一个系列:
或者使用自定义函数:
输出:
You can use comparison and boolean indexing per row, save the intermediate variable using assignment expression, and create a Series:
Or using a custom function:
output: