【问题标题】:change a block of row values in a column in one go一次性更改列中的一组行值
【发布时间】:2018-03-31 22:10:15
【问题描述】:

我想一次性更改一列中的连续行值块。

最后我得到了这个完成工作的方法,但我有点认为必须有一个更整洁的方法。

df = pd.read_excel('ja.xlsx')
df  # echo the contents of the df

for index, row in df.iterrows():
    if index < 4:
        df.loc[ index, "my_col_name"] = 'yes'
    else:
        df.loc[ index, "my_col_name"] = 'no'

有什么建议吗?

谢谢!

【问题讨论】:

标签: pandas


【解决方案1】:
df["my_col_name"]='yes'
df.loc[df.index>=4,"my_col_name"]='no'

df['new1']=np.where(df.index>=4,'no','yes')

【讨论】:

  • 我知道会有更好的方法 - 事实证明有两种更好的方法 :-) 尽管在线边缘它为了简洁而没有任何混淆。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-20
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 2019-02-05
相关资源
最近更新 更多