Home>

I want to delete the data in the line after the specified index and save it.
Also, I want to do it while increasing the index one by one.
I tried to put a variable in the drop using a for statement, but it didn't work.
If i find any mistakes, thank you.

Error message
File "<ipython-input-41-5271bf27d6da>", line 23
    df_2 = df_1.drop ([i:])
                                        ^
SyntaxError: invalid syntax
Applicable source code
for i in range (index, len (df_1.index)):
    df_2 = df_1.drop ([i:])
    #Save
    df_2.to_csv ('hoge _ {}. csv'.format (i))

Delete [] in drop

  • Answer # 1

    import pandas as pd
    df = pd.DataFrame ({'value': range (10)}, index = range (0,20,2))
    # value
    # 0 0
    #twenty one
    # 4 2
    # 6 3
    # 8 4
    # 10 5
    # 12 6
    # 14 7
    # 16 8
    # 18 9
    # delete after index = 6
    df.drop (df.loc [6:]. index)
    # value
    # 0 0
    #twenty one
    # 4 2
    # Delete after line 6
    df.drop (df.iloc [6:]. index)
    # value
    # 0 0
    #twenty one
    # 4 2
    # 6 3
    # 8 4
    # 10 5
    # Process in loop
    for i in range (10):
        ret = df.drop (df.iloc [i:]. index)