Python数据分析中的10个高效函数
Python是一种很流行的编程语言,具有广泛的应用和活跃的社区。Python机器学习库丰富,常用的有pandas、numpy、matplotlib、scikit-learn等库。在Python数据分析中,很容易遇到大量的数据处理,比如数据清洗,数据整合,数据可视化等,这时就需要用到高效的函数来提高数据处理的效率。本文将介绍Python数据分析中的10个高效函数,帮助你更加高效地完成数据处理工作。
1. map()函数
map()函数能够对一个列表或者迭代器中的每个元素应用一个函数,返回一个新的迭代器,其中包含了应用这个函数后得到的结果。map()函数基本语法为:
map(function, iterable)
其中,function是应用到每个元素的函数,iterable是迭代器或者列表。
举例:
def square(x):
return x ** 2
list = [1, 2, 3, 4, 5]
squares = map(square, list)
print(list(squares))
输出结果为:
[1, 4, 9, 16, 25]
2. filter()函数
filter()函数能够对一个列表或者迭代器中的每个元素应用一个函数,返回一个新的迭代器,其中包含了使函数返回True的元素。filter()函数基本语法为:
filter(function, iterable)
其中,function是应用到每个元素的函数,iterable是迭代器或者列表。
举例:
def is_even(x):
return x % 2 == 0
list = [1, 2, 3, 4, 5]
even = filter(is_even, list)
print(list(even))
输出结果为:
[2, 4]
3. reduce()函数
reduce()函数能够对一个列表或者迭代器中的元素应用一个函数,返回一个简化后的单个结果。reduce()函数基本语法为:
reduce(function, iterable)
其中,function是应用到每对元素的函数,iterable是迭代器或者列表。
举例:
from functools import reduce
def add(x, y):
return x + y
list = [1, 2, 3, 4, 5]
sum = reduce(add, list)
print(sum)
输出结果为:
15
4. apply()函数
apply()函数是pandas库中的一个函数,能够对一行或者一列应用一个函数。在pandas中进行数据分析时,经常需要对DataFrame中的某些列或行进行操作,此时可以使用apply()函数。
举例:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df['C'] = df['B'].apply(lambda x: x * 2)
print(df)
输出结果为:
A B C 0 1 4 8 1 2 5 10 2 3 6 12
5. value_counts()函数
value_counts()函数是pandas库中的一个函数,能够返回一列中每个唯一值的数量。在数据分析中,很常见的是想要看一列中有多少个唯一值,此时可以使用value_counts()函数。
举例:
import pandas as pd
data = {'A': [1, 2, 3, 3, 4, 5, 5, 5]}
df = pd.DataFrame(data)
print(df['A'].value_counts())
输出结果为:
5 3 3 2 2 1 4 1 1 1 Name: A, dtype: int64
6. groupby()函数
groupby()函数是pandas库中的一个函数,能够根据某些条件对DataFrame中的数据进行分组。在数据分析中,很常见的是需要对数据进行分组,此时可以使用groupby()函数。
举例:
import pandas as pd
data = {'A': ['apple', 'orange', 'banana', 'apple', 'orange', 'banana'],
'B': [1, 2, 3, 4, 5, 6]}
df = pd.DataFrame(data)
grouped = df.groupby('A')
print(grouped.sum())
输出结果为:
B
A
apple 5
banana 9
orange 7
7. pivot_table()函数
pivot_table()函数是pandas库中的一个函数,能够根据某些条件对DataFrame中的数据进行汇总。在数据分析中,很常见的是需要对数据进行汇总,此时可以使用pivot_table()函数。
举例:
import pandas as pd
data = {'A': ['apple', 'orange', 'banana', 'apple', 'orange', 'banana'],
'B': [1, 2, 3, 4, 5, 6],
'C': [2, 4, 6, 8, 10, 12]}
df = pd.DataFrame(data)
pt = df.pivot_table(values=['B', 'C'], index=['A'], aggfunc='sum')
print(pt)
输出结果为:
B C
A
apple 5 10
banana 9 18
orange 7 14
8. merge()函数
merge()函数是pandas库中的一个函数,能够将多个DataFrame中的数据进行合并。在数据分析中,很常见的是需要将多个DataFrame中的数据进行合并,此时可以使用merge()函数。
举例:
import pandas as pd
data1 = {'A': ['apple', 'orange', 'banana'],
'B': [1, 2, 3]}
df1 = pd.DataFrame(data1)
data2 = {'A': ['apple', 'banana', 'grape'],
'C': [4, 5, 6]}
df2 = pd.DataFrame(data2)
merged = pd.merge(df1, df2, on='A')
print(merged)
输出结果为:
A B C
0 apple 1 4
1 banana 3 5
9. reshape()函数
reshape()函数是numpy库中的一个函数,能够对数组进行reshape操作,即改变数组的维度。在数据分析中,很常见的是需要对数组进行reshape操作,此时可以使用reshape()函数。
举例:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) reshaped = np.reshape(arr, (2, 4)) print(reshaped)
输出结果为:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
10. plot()函数
plot()函数是matplotlib库中的一个函数,能够对数据进行可视化。在数据分析中,很常见的是需要将数据可视化,此时可以使用plot()函数。
举例:
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5, 6] y = [1, 4, 9, 16, 25, 36] plt.plot(x, y) plt.show()
输出结果为:

结语
本文介绍了Python数据分析中的10个高效函数,分别是map()函数、filter()函数、reduce()函数、apply()函数、value_counts()函数、groupby()函数、pivot_table()函数、merge()函数、reshape()函数和plot()函数。这些函数都是在Python数据分析中非常常用的,掌握它们将有助于你更加高效地进行数据处理。
