定义函数 返回pd.DataFrame表
时间: 2023-07-25 21:04:18 浏览: 47
可以使用Python中的pandas库来创建和操作数据框(DataFrame)。
下面是一个示例函数,它返回一个包含两列数据的数据框:
```python
import pandas as pd
def create_dataframe():
data = {'Name': ['John', 'Emily', 'Jack', 'Sophia'],
'Age': [27, 22, 24, 29]}
df = pd.DataFrame(data)
return df
```
在上面的函数中,我们首先定义了一个字典数据,其中包含两个键值对,分别对应了“Name”和“Age”这两列数据。然后,我们使用pd.DataFrame()函数将字典数据转换为一个数据框,并将其存储在变量df中。最后,我们返回这个数据框。
要使用这个函数,只需要调用它并将其输出:
```python
df = create_dataframe()
print(df)
```
这将输出以下结果:
```
Name Age
0 John 27
1 Emily 22
2 Jack 24
3 Sophia 29
```
相关问题
append函数向pd.DataFrame()中添加元素
`append`函数可以用来向`pd.DataFrame()`中添加元素,具体方法如下:
1. 创建一个空的`pd.DataFrame()`,并指定列名:
```python
import pandas as pd
df = pd.DataFrame(columns=['col1', 'col2'])
```
2. 定义一个字典,表示待添加的元素:
```python
new_row = {'col1': 1, 'col2': 'a'}
```
3. 使用`append`函数,将待添加的元素添加到`pd.DataFrame()`中:
```python
df = df.append(new_row, ignore_index=True)
```
其中,`ignore_index=True`参数表示忽略原来的索引,重新生成新的索引。
可以多次使用`append`函数,向`pd.DataFrame()`中不断添加元素。
pd.DataFrame
`pd.DataFrame` 是 Pandas 库中的一个类,用于创建二维的数据结构,类似于 Excel 表格。它可以接收多种输入数据类型,包括列表、字典、二维数组、Series 等,通过指定列名和行索引,将数据转化为表格形式。以下是一个简单的示例:
```python
import pandas as pd
data = {'name': ['Alice', 'Bob', 'Charlie', 'David'],
'age': [25, 30, 35, 40],
'gender': ['F', 'M', 'M', 'M']}
df = pd.DataFrame(data)
print(df)
```
输出结果为:
```
name age gender
0 Alice 25 F
1 Bob 30 M
2 Charlie 35 M
3 David 40 M
```
在这个例子中,我们先定义了一个字典 `data`,其中包含三个键值对,分别为 `name`、`age` 和 `gender`。然后,我们使用 `pd.DataFrame` 函数将这个字典转化为一个 DataFrame 对象 `df`,并打印输出。在输出结果中,每一列的列名为字典中的键,每一行的行索引为默认的整数序列。
除了字典以外,我们还可以使用其他数据类型来创建 DataFrame,例如:
```python
import pandas as pd
# 使用列表创建 DataFrame
data = [['Alice', 25, 'F'], ['Bob', 30, 'M'], ['Charlie', 35, 'M'], ['David', 40, 'M']]
df = pd.DataFrame(data, columns=['name', 'age', 'gender'])
# 使用二维数组创建 DataFrame
import numpy as np
data = np.random.rand(4, 3)
df = pd.DataFrame(data, columns=['A', 'B', 'C'], index=['a', 'b', 'c', 'd'])
# 使用 Series 创建 DataFrame
s1 = pd.Series(['Alice', 'Bob', 'Charlie', 'David'], name='name')
s2 = pd.Series([25, 30, 35, 40], name='age')
s3 = pd.Series(['F', 'M', 'M', 'M'], name='gender')
df = pd.concat([s1, s2, s3], axis=1)
```
这些方法都可以用来创建 DataFrame,其中第一个例子和我们之前的例子类似,只不过使用了列表来构造数据。第二个例子中,我们使用了 NumPy 库中的随机数函数来生成一个 4 行 3 列的二维数组,然后指定了列名和行索引。第三个例子中,我们先定义了三个 Series 对象,分别表示名字、年龄和性别,然后使用 `pd.concat` 函数将它们合并成一个 DataFrame。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)