dataframe.md 4.0 KB
Newer Older
F
fix bug  
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
# pandas 创建dataframe

创建dataframe的几种方式

```python
import pandas as pd
import numpy as np


def create_dataframe_from_list():
    # TODO(You):请在此编写代码
    return data_df


def create_dataframe_from_series():
    # TODO(You):请在此编写代码
    return data_df


def create_dataframe_from_2darray():
    # TODO(You):请在此编写代码
    return data_df


def create_dataframe_from_dict():
   # TODO(You):请在此编写代码
    return data_df


if __name__ == '__main__':
    print('由数组list组成的字典创建dataframe:')
    data_df = create_dataframe_from_list()
    print(data_df)

    print('由Series组成的字典创建dataframe:')
    data_df = create_dataframe_from_series()
    print(data_df)

    print('通过二维数组直接创建dataframe:')
    data_df = create_dataframe_from_2darray()
    print(data_df)

    print('由字典组成的列表创建dataframe:')
    data_df = create_dataframe_from_dict()
    print(data_df)
```

请选出下列对**TODO**部分实现,**描述错误** 的选项。

## template

```python
import pandas as pd
import numpy as np


def create_dataframe_from_list():
    data = {
        'cloumn_one': np.random.randint(0, 10, size=5),
        'cloumn_two': np.random.randint(0, 10, size=5),
        'cloumn_three': np.random.randint(0, 10, size=5)
    }
    data_df = pd.DataFrame(data)
    return data_df


def create_dataframe_from_series():
    data = {
        'cloumn_one': pd.Series(np.random.randint(0, 10, size=5)),
        'cloumn_two': pd.Series(np.random.randint(0, 10, size=5)),
        'cloumn_three': pd.Series(np.random.randint(0, 10, size=5))
    }
    data_df = pd.DataFrame(data)
    return data_df


def create_dataframe_from_2darray():
    data = np.random.randint(0, 10, size=9).reshape(3, 3)
    data_df = pd.DataFrame(
        data, columns=['cloumn_one', 'cloumn_two', 'cloumn_three'])
    return data_df


def create_dataframe_from_dict():
    data = [{'cloumn_one': 1, 'cloumn_two': 2, 'cloumn_three': 3},
            {'cloumn_one': 5, 'cloumn_two': 6, 'cloumn_three': 7}]
    data_df = pd.DataFrame(data)
    return data_df


if __name__ == '__main__':
    print('由数组list组成的字典创建dataframe:')
    data_df = create_dataframe_from_list()
    print(data_df)

    print('由Series组成的字典创建dataframe:')
    data_df = create_dataframe_from_series()
    print(data_df)

    print('通过二维数组直接创建dataframe:')
    data_df = create_dataframe_from_2darray()
    print(data_df)

    print('由字典组成的列表创建dataframe:')
    data_df = create_dataframe_from_dict()
    print(data_df)
```

## 答案

```python
# 该函数的输出数据只含有 cloumn_one  cloumn_two  cloumn_three 三个列
def create_dataframe_from_dict():
    data = [{'cloumn_one': 1, 'cloumn_two': 2, 'cloumn_three': 3},
            {'cloumn_one': 5, 'cloumn_two': 6, 'cloumn_xxx': 7}]
    data_df = pd.DataFrame(data)
    return data_df
```

## 选项

### A

```python
# 该函数的输出数据只含有 cloumn_one  cloumn_two  cloumn_three 三个列
def create_dataframe_from_list():
    data = {
        'cloumn_one': np.random.randint(0, 10, size=5),
        'cloumn_two': np.random.randint(0, 10, size=5),
        'cloumn_three': np.random.randint(0, 10, size=5)
    }
    data_df = pd.DataFrame(data)
    return data_df
```

### B

```python
# 该函数的输出数据只含有 cloumn_one  cloumn_two  cloumn_three 三个列
def create_dataframe_from_series():
    data = {
        'cloumn_one': pd.Series(np.random.randint(0, 10, size=5)),
        'cloumn_two': pd.Series(np.random.randint(0, 10, size=5)),
        'cloumn_three': pd.Series(np.random.randint(0, 10, size=5))
    }
    data_df = pd.DataFrame(data)
    return data_df
```

### C

```python
# 该函数的输出数据只含有 cloumn_one  cloumn_two  cloumn_three 三个列
def create_dataframe_from_2darray():
    data = [
        [3, 5, 2],
        [2, 7, 9],
        [5, 6, 9]
    ]
    data_df = pd.DataFrame(
        data, columns=['cloumn_one', 'cloumn_two', 'cloumn_three'])
    return data_df
```