pd.dataframe index code example

Example 1: pandas index

How to add index to the DataFrame at runtime

df["patient"] = ["patient-" + str( i ) for i in range( max( df.index ) + 1  )]
df.set_index( "patient", inplace=True )
---------------------------------------------------------------------                 
patient = pandas.Series( ["patient-" + str( i ) for i in range( max( df.index ) + 1 )] )
df.index = patient
---------------------------------------------------------------------
df.index = ["patient-" + str( i ) for i in range( max( df.index ) + 1 )]

Example 2: pandas df by row index

indices = [133, 22, 19, 203, 14, 1]
df_by_indices = df.iloc[indices, :]

Example 3: df.index

The index (row labels) of the DataFrame.

Example 4: pandas row data to webform

def _getFields(obj, tree=None, retval=None, fileobj=None):
    fieldAttributes = {'/FT': 'Field Type', '/Parent': 'Parent', '/T': 'Field Name', 
    '/TU': 'Alternate Field Name', '/TM': 'Mapping Name', '/Ff': 'Field Flags', 
    '/V': 'Value', '/DV': 'Default Value'}
    if retval is None:
        retval = OrderedDict()
        catalog = obj.trailer["/Root"]
        if "/AcroForm" in catalog:
            tree = catalog["/AcroForm"]
        else:
            return None
    if tree is None:
        return retval

    obj._checkKids(tree, retval, fileobj)
    for attr in fieldAttributes:
        if attr in tree:
            obj._buildField(tree, retval, fileobj, fieldAttributes)
            break

    if "/Fields" in tree:
        fields = tree["/Fields"]
        for f in fields:
            field = f.getObject()
            obj._buildField(field, retval, fileobj, fieldAttributes)

    return retval

Tags:

C Example