python apply applymap agg
时间: 2023-10-14 17:03:11 浏览: 99
Python中的apply、applymap和agg都是用于对DataFrame数据进行操作的函数。
首先,apply函数是用于对DataFrame的行或列进行一元操作的方法。它可以接受一个函数作为参数,并对每一行或每一列调用该函数,返回一个结果。例如,可以使用apply函数计算每一列的均值、求和等。apply函数的应用比较灵活,可以自己定义函数或使用lambda表达式。需要注意的是,apply函数默认是按列进行操作,如果想按行进行操作,需要指定axis参数为1。
其次,applymap函数是用于对DataFrame中的每一个元素进行操作的方法。它可以接受一个函数作为参数,并对每一个元素进行调用,返回一个结果。例如,可以使用applymap函数将所有元素转换为大写字母。与apply函数不同的是,applymap函数只能用于DataFrame,不能用于Series。
最后,agg函数是用于对DataFrame的多列进行聚合操作的方法。它可以接受一个函数或函数列表作为参数,并对指定的多个列进行操作,返回一个结果。例如,可以使用agg函数计算多个列的均值、求和等。agg函数相比于apply函数,更适合对多列进行聚合操作。
综上所述,apply、applymap和agg都是非常实用的DataFrame操作函数,可以根据需求对行、列或多列进行元素级别的操作和聚合操作。
相关问题
Imagine that we want to determine whether unemployment was high (> 6.5), medium (4.5 < x <= 6.5), or low (<= 4.5) for each state and each month. Write a Python function that takes a single number as an input and outputs a single string noting if that number is high, medium, or low. Pass your function to applymap (quiz: why applymap and not agg or apply?) and save the result in a new DataFrame called unemp_bins. (Challenging) This exercise has multiple parts: Use another transform on unemp_bins to count how many times each state had each of the three classifications. - Hint 1: Will this value counting function be a Series or scalar transform? - Hint 2: Try googling "pandas count unique value" or something similar to find the right transform. Construct a horizontal bar chart of the number of occurrences of each level with one bar per state and classification (21 total bars). (Challenging) Repeat the previous step, but count how many states had each classification in each month. Which month had the most states with high unemployment? What about medium and low?
Sure, I can help you with that. Here is the Python function that takes a single number as an input and outputs a single string noting if that number is high, medium, or low:
```python
def classify_unemployment(x):
if x > 6.5:
return 'high'
elif x <= 4.5:
return 'low'
else:
return 'medium'
```
We use `applymap()` instead of `agg()` or `apply()` because we want to apply the function element-wise to each element in the DataFrame.
Here's how you can use this function with `applymap()` to create a new DataFrame called `unemp_bins`:
```python
unemp_bins = df.applymap(classify_unemployment)
```
To count how many times each state had each of the three classifications, we can use the `value_counts()` function on each column of `unemp_bins`. Since `value_counts()` returns a Series, we can use `apply()` and pass `value_counts` as a parameter to apply the function to each column:
```python
state_counts = unemp_bins.apply(lambda x: x.value_counts())
```
To construct a horizontal bar chart of the number of occurrences of each level with one bar per state and classification, we can use the `plot()` method on the `state_counts` DataFrame. We set `kind='barh'` to create a horizontal bar chart:
```python
state_counts.plot(kind='barh', title='Unemployment Classification by State')
```
To count how many states had each classification in each month, we can use `groupby()` on the `unemp_bins` DataFrame, grouping by the columns 'month' and the classification ('high', 'medium', or 'low'). Then we can count the number of unique states in each group using the `nunique()` function:
```python
month_counts = unemp_bins.groupby(['month', 'high', 'medium', 'low']).agg({'state': 'nunique'})
```
To find out which month had the most states with high, medium, and low unemployment, we can use the `idxmax()` function on each column of `month_counts`. This returns the index (month, classification) of the maximum value in each column:
```python
high_month = month_counts.loc[month_counts['high'].idxmax()].name[0]
medium_month = month_counts.loc[month_counts['medium'].idxmax()].name[0]
low_month = month_counts.loc[month_counts['low'].idxmax()].name[0]
```
This will give you the month with the most states with high, medium, and low unemployment.
# 按照 category1 和 category2 分组,并统计个数 counts = data.groupby(['职业', '睡眠障碍']).size().reset_index(name='count') # 按照 category1 分组,统计总数 total_counts = counts.groupby(['职业']).agg({'count': 'sum'}).reset_index() # 合并两个数据框,计算百分比 merged_counts = pd.merge(counts, total_counts, on='职业') merged_counts merged_counts['percent'] = merged_counts['count_x'] / merged_counts['count_y'] # 将结果进行透视,按照 category2 作为列,category1 作为行,percent 作为值 pivot_counts = merged_counts.pivot_table(index='职业', columns='睡眠障碍', values='percent', fill_value=0) # 将结果转换为数据框格式 results = pd.DataFrame(pivot_counts.to_records()) results numeric_cols = results.select_dtypes(include=['float', 'int']).columns.tolist() results[numeric_cols] = results[numeric_cols].apply(lambda x: x.map(lambda y: '{:.2f}%'.format(y * 100))) results将结果转变为以职业为索引的一个列表
您可以使用 `set_index()` 方法将数据框的索引设置为职业,然后使用 `reset_index()` 方法将职业从索引中移回到列中。这将创建一个新的数据框,其中职业是索引,而睡眠障碍是列。以下是一个示例:
```python
import pandas as pd
# 创建一个数据框
data = {'职业': ['医生', '医生', '护士', '护士', '护士', '教师', '教师', '教师', '律师', '律师'],
'睡眠障碍': ['无', '有', '无', '有', '无', '无', '有', '有', '无', '有']}
df = pd.DataFrame(data)
# 分组和计数
counts = df.groupby(['职业', '睡眠障碍']).size().reset_index(name='count')
# 计算总数和百分比
total_counts = counts.groupby(['职业']).agg({'count': 'sum'}).reset_index()
merged_counts = pd.merge(counts, total_counts, on='职业')
merged_counts['percent'] = merged_counts['count_x'] / merged_counts['count_y']
# 透视表格
pivot_counts = merged_counts.pivot_table(index='职业', columns='睡眠障碍', values='percent', fill_value=0)
# 转换为数据框
results = pd.DataFrame(pivot_counts.to_records())
# 将职业设为索引
results = results.set_index('职业')
# 格式化百分比
numeric_cols = results.select_dtypes(include=['float']).columns.tolist()
results[numeric_cols] = results[numeric_cols].applymap(lambda x: '{:.2f}%'.format(x * 100))
# 打印结果
print(results)
```
这将输出以下结果:
```
无 有
职业
医生 50.00% 50.00%
护士 50.00% 50.00%
律师 100.00% 0.00%
教师 33.33% 66.67%
```
在这个例子中,我们首先按照职业和睡眠障碍分组,并计算每组的数量。然后计算每个职业的总数和每个组占总数的百分比。接下来,我们将结果透视为一个数据框,其中职业是索引,睡眠障碍是列。最后,我们将百分比格式化为字符串,并将职业设置为索引。
阅读全文