pkrish
pkrish

Reputation: 71

print column position of a dataframe

this is my dataframe:

c_id  string1  age  salary   string2
1      apple    21    21.22   hello_world
2      orange   41   23.4     world
3      kiwi     81   20.22    hello

i need to print the string value which has max_len along with the column datatype, name and its position.so my expected output should be:

position c_name   c_dtype  max_len
1        string1   object  orange
4        string2   object  hello_world

i tried these concept to print string value based on its max length.

for col in df.select_dtypes([np.object]):
    max_len = max(df[col], key=len)
    print('prints col_name:', col) 
    print('prints the datatype ',df[col].dtype)
    print('prints the maximum length string value',max_len)

i need to merge all these and should get my expected output as mentioned above.

Upvotes: 1

Views: 505

Answers (1)

jezrael
jezrael

Reputation: 863236

Use Index.get_loc for position of column:

out = []    
for col in df.select_dtypes([np.object]):
    max_len = max(df[col], key=len)
    print('position:', df.columns.get_loc(col))  
    print('prints col_name:', col) 
    print('prints the datatype ',df[col].dtype)
    print('prints the maximum length string value',max_len)

    out.append({'position':df.columns.get_loc(col), 
                'c_name': col, 'c_dtype':df[col].dtype, 'max_len': max_len})

df1 = pd.DataFrame(out)
print (df1)
   position   c_name c_dtype      max_len
0         1  string1  object       orange
1         4  string2  object  hello_world

List comprehension solution:

out = [{'position':df.columns.get_loc(col), 
        'c_name': col, 'c_dtype':df[col].dtype, 'max_len': max(df[col], key=len)} 
        for  col in df.select_dtypes([np.object])]

df1 = pd.DataFrame(out)
print (df1)
   position   c_name c_dtype      max_len
0         1  string1  object       orange
1         4  string2  object  hello_world

Upvotes: 1

Related Questions