Python Pandas dataframe.add_prefix()
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Dataframe.add_prefix()函数既可用于系列,也可用于数据框架。
- 对于系列,行标签是有前缀的。
- 对于DataFrame,列的标签是有前缀的。
语法: DataFrame.add_prefix(prefix)
参数:
prefix : string
返回值: with_prefix: type of caller
例子#1:数据框架中每一列的前缀col。
# importing pandas as pd
import pandas as pd
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
# Printing the first 10 rows of the
# dataframe for visualization
df[:10]
# Using add_prefix() function
# to add 'col_' in each column label
df = df.add_prefix('col_')
# Print the dataframe
df
输出:
示例#2:在pandas中使用add_prefix()与系列。
add_prefix()在系列的情况下改变了行索引标签。
# importing pandas as pd
import pandas as pd
# Creating a Series
df = pd.Series([1, 2, 3, 4, 5, 10, 11, 21, 4])
# This will prefix 'Row_' in
# each row of the series
df = df.add_prefix('Row_')
# Print the Series
df
输出: