python - 在一个单元格之后删除所有单元格
问题描述
您好,我有以下 DataFrame df:
A B C D E F
apple 0 red green blue 8
orange 2 red blue white 10
apple 2 red green blue 8
orange 0 red 20 purple 10
我想将某个单元格之后的所有单元格更改为空字符串。
我试过下面的代码,想知道是否有更有效的方法来做到这一点
for i in range(len(df['B']):
if df['B'].iloc[i] == 0:
df['C'].iloc[i] = ""
df['D'].iloc[i] = ""
df['E'].iloc[i] = ""
df['F'].iloc[i] = ""
A B C D E F
apple 0
orange 2 red blue white 10
apple 2 red green blue 8
orange 0
谢谢
解决方案
尝试:
df.loc[df['B']==0,'C': ] = ''
印刷:
A B C D E F
0 apple 0
1 orange 2 red blue white 10
2 apple 2 red green blue 8
3 orange 0
推荐阅读
- formik - 是的:如何验证两个相互依赖的日期?
- javascript - 如何在 React 中使用 HOC 传递静态 navigationOptions?
- html - 无法在 Asp.net core web api 中保存 html 代码
- xml - strings.xml:这有什么不正确的?
- python-3.x - 使用线程时如何解决winerror32?
- sql - SQL - 从课程和主题中过滤
- javascript - 为什么 array.forEach() 在这段代码中什么都不做?
- github - 新团队成员的 GitHub 安全问题
- reactjs - 被 CORS 策略阻止:“Access-Control-Allow-Origin”
- c - C - 使用 while 和 switch/case 的菜单程序返回无效选择的重复菜单