【问题标题】:How to add item last and remove first item in python dataframe?How to add item last and remove first item in python dataframe?
【发布时间】:2022-12-02 03:17:06
【问题描述】:

My dataframe is like this:

data = {
  "a": [420, 380, 390],
  "b": [50, 40, 45]
}

df = pd.DataFrame(data)

I want to add new item at the end of this dataframe, and remove the first item. I mean cont will be 3 each addition.

New item add

{"a": 300, b: 88}

and last stuation will be:

data = {
  "a": [380, 390, 300],
  "b": [40, 45, 88]
}

Is there a short way to do this?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    Try this:

    dct = {"a": 300, "b": 88}
    df_new = df.append(dct, ignore_index=True).drop(0, axis=0).reset_index(drop=True)
    print(df_new)
    

    Output:

         a   b
    0  380  40
    1  390  45
    2  300  88
    

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 2022-12-28
      • 2022-12-01
      • 2022-12-01
      • 2022-12-26
      • 2022-12-27
      • 2022-12-26
      • 2022-12-02
      • 1970-01-01
      相关资源
      最近更新 更多