Python | Pandas dataframe.add_prefix()
Last Updated :
16 Nov, 2018
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Dataframe.add_prefix()
function can be used with both series as well as dataframes.
- For Series, the row labels are prefixed.
- For DataFrame, the column labels are prefixed.
Syntax: DataFrame.add_prefix(prefix)
Parameters:
prefix : string
Returns: with_prefix: type of caller
For link to CSV file Used in Code, click here
Example #1: Prefix col_
in each columns in the dataframe
import pandas as pd
df = pd.read_csv( "nba.csv" )
df[: 10 ]
|
df = df.add_prefix( 'col_' )
df
|
Output:
Example #2: Using add_prefix()
with Series in pandas
add_prefix()
alters the row index labels in the case of series.
import pandas as pd
df = pd.Series([ 1 , 2 , 3 , 4 , 5 , 10 , 11 , 21 , 4 ])
df = df.add_prefix( 'Row_' )
df
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...