简体   繁体   中英

Increment a value of column in pandas/csv file when the row is appended in python

I have this code which selects a column from a csv file and appends it as a row to another csv file:

def append_pandas(s,d):
    import pandas as pd
    df = pd.read_csv(s, sep=';', header=None)
    df_t = df.T
    df_t.iloc[0:1, 0:1] = 'Time Point'
    df_t.columns = df_t.iloc[0]
    df_new = df_t.drop(0)
    pdb = pd.read_csv(d, sep=';')
    newpd = pdb.append(df_new)
    from pandas import DataFrame
    newpd.to_csv(d, sep=';')

As you can see, there is a Time Point column, and every time the row is appended, I want the value in this column to increment by 1. For example, when the first row is appended, it is 0, the second row will have 1, the third row will have 3 etc.

Could you please help with this?

The resulting file looks like this:

Time Point     A   B   C ...
         1    23  65  98  
         2    10  24  85
         3     1  54  72
         4    33  77   0 
         5     7  73  81
         6   122  43   5 # <- row added with new Time Point

PS The Row which is being appended doesn't have a Time Point value and looks like this:

   Well ID   Cell Count
         A          100
         B          200 
         C           54
         D           77
         E           73
         F           49

The resulting file shouldn't have the headers of the first file added either ('Well ID','Cell Count'); so just the values of the 'Cell Count' column.

Please, help:(

Try the following code and check the output CSV file:

import io
import pandas as pd

# Load an empty CSV file and read it as dataframe
empty_csv = 'Time Point;A;B;C;D;E;F'
df = pd.read_csv(io.StringIO(empty_csv), sep=';')

# Load a CSV file which will be added to `df` defined above
add_csv = 'Well ID;Cell Count\nA;100\nB;200\nC;54\nD;77\nE;73\nF;49\n'
df_add = pd.read_csv(io.StringIO(add_csv), sep=';')

def append_a_row(df, df_add):
    df_add = df_add.set_index('Well ID').T
    df_add.insert(0, 'Time Point', len(df)+1)
    return df.append(df_add)

df_new = append_a_row(df, df_add)

# Save as csv
d = 'path_to_the_output.csv'
df_new.to_csv(d, sep=';', index=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM