python dataframe .count
时间: 2023-10-19 21:24:44 浏览: 40
在 Python 的 pandas 库中,DataFrame.count() 方法可以返回每列非空值的数量。该方法将返回一个 Series,其中包含每列的计数值。以下是一个示例:
```python
import pandas as pd
# 创建一个 DataFrame
data = {'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'age': [25, 32, None, 19, 27],
'gender': ['F', 'M', 'M', 'M', 'F']}
df = pd.DataFrame(data)
# 使用 count() 方法计算每列的非空值数量
counts = df.count()
print(counts)
```
输出结果为:
```
name 5
age 4
gender 5
dtype: int64
```
这表明 `name`、`age` 和 `gender` 列中分别有 5、4 和 5 个非空值。注意,由于 `age` 列中有一个空值,因此它的计数值为 4。
相关问题
statistical summary of the dataframe.
Sure, I can provide you with a statistical summary of a dataframe. In Python, you can use the describe() method to get a statistical summary of a dataframe. Here is an example code snippet:
```
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10], 'C': [3, 6, 9, 12, 15]})
# get the statistical summary of the dataframe
summary = df.describe()
# print the summary
print(summary)
```
This will output the following statistical summary of the dataframe:
```
A B C
count 5.000000 5.000000 5.000000
mean 3.000000 6.000000 9.000000
std 1.581139 3.162278 4.743416
min 1.000000 2.000000 3.000000
25% 2.000000 4.000000 6.000000
50% 3.000000 6.000000 9.000000
75% 4.000000 8.000000 12.000000
max 5.000000 10.000000 15.000000
```
This summary provides information such as the count, mean, standard deviation, minimum, maximum, and quartiles for each column in the dataframe.
pd.DataFrame.groupby
`pd.DataFrame.groupby` 是 Pandas 库中用于对 DataFrame 进行分组的函数。它基于一个或多个键(可以是列名、函数或数组)将 DataFrame 拆分成多个组,并将每个组应用函数(如 sum、mean、count 等)进行聚合。使用 `groupby` 函数可以方便地进行数据的分析和探索。
例如,下面的代码将根据 "region" 列对 DataFrame 进行分组,并计算每个组中 "sales" 列的总和:
```python
import pandas as pd
df = pd.DataFrame({
'region': ['East', 'West', 'North', 'South', 'East', 'West', 'North', 'South'],
'sales': [100, 200, 150, 50, 120, 180, 80, 70],
'profit': [10, 20, 15, 5, 12, 18, 8, 7]
})
grouped = df.groupby('region')
total_sales = grouped['sales'].sum()
print(total_sales)
```
输出结果为:
```
region
East 220
North 230
South 120
West 380
Name: sales, dtype: int64
```
这里分别计算了每个地区的销售总额。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)