Home > Mobile >  Print Elements from Data Frame
Print Elements from Data Frame

Time:07-12

I have the following code:

import pandas as pd
import numpy as np

df = {'sport' : ['football', 'hockey', 'baseball', 'basketball', 'nan'], 'league': ['NFL', 'NHL', 'MLB', 'NBA', 'NaN'], 'number': [1,2,3,4,'']}
df = pd.DataFrame(df)
df

def printlinesindf(datafrm):
    print('This is my favrotie sport:', df[0], 'and this is my favorite league: ',df[1], 'and this is my ranking: ', df[0]')
    
printlinesindf(df)

This produces an error; I'd like to print each element of the row somehow using the apply function rather than a simple for loop statement. I do not want to include the header (i.e., sport, league, number).

Example expected output for line one this is my favorite sport: football, this is my favorite league: NFL, this is my ranking: 1

CodePudding user response:

If you want to use apply you can do something like this:

def printlinesindf(row):
    print('This is my favrotie sport:', row[0], 'and this is my favorite league: ',row[1], 'and this is my ranking: ', row[2])
    
df.apply(lambda row: printlinesindf(row), axis=1)

However, this is not faster than a for loop.

CodePudding user response:

For future needs, you may want to generate a column with these values and then do the single print:

import pandas as pd

df = {
    'sport' : ['football', 'hockey', 'baseball', 'basketball'], 
    'league': ['NFL', 'NHL', 'MLB', 'NBA'], 
    'number': [1,2,3,4]
}
df = pd.DataFrame(df)

def printlinesindf(datafrm):
    datafrm['joined'] = 'This is my favorite sport: '   datafrm['sport']   \
        ' and this is my favorite league: '   datafrm['league']   \
            ' and this is my ranking: '   datafrm['number'].astype(str)
    # print one string of all the rows in the dataframe separate with a newline
    print(datafrm['joined'].str.cat(sep='\n')) 

printlinesindf(df)

I took into account that you want to generate a single print with all the lines, now if you want a print for each line, there are other approaches such as:

import pandas as pd

df = {
    'sport' : ['football', 'hockey', 'baseball', 'basketball'], 
    'league': ['NFL', 'NHL', 'MLB', 'NBA'], 
    'number': [1,2,3,4]
}
df = pd.DataFrame(df)

def printlinesindf(datafrm):
    datafrm['joined'] = 'This is my favorite sport: '   datafrm['sport']   \
        ' and this is my favorite league: '   datafrm['league']   \
            ' and this is my ranking: '   datafrm['number'].astype(str)
    # print each row of the dataframe 'joined' column
    for a in datafrm['joined']:
        print(a)

printlinesindf(df)

Added doubt (I also updated the code to show...) after editing the original question:

import pandas as pd

df = {
    'sport' : ['football', 'hockey', 'baseball', 'basketball'], 
    'league': ['NFL', 'NHL', 'MLB', 'NBA'], 
    'number': [1,2,3,'']
}
df = pd.DataFrame(df)
# remove lines with empty values
df = df[df['number'] != '']
  • Related