From 002675a991e13143cfeeae9af420973381e00559 Mon Sep 17 00:00:00 2001 From: feilong Date: Wed, 16 Feb 2022 11:59:07 +0800 Subject: [PATCH] fix bug --- .../encode.md" | 15 +- .../dataframe.json" | 8 +- .../dataframe.md" | 163 ++++++++++++++++++ .../dataframe.py" | 86 ++++----- 4 files changed, 224 insertions(+), 48 deletions(-) create mode 100644 "data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.md" diff --git "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.md" "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.md" index 77ca330..dd9296b 100644 --- "a/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.md" +++ "b/data/1.python\345\210\235\351\230\266/3.\350\277\233\351\230\266\350\257\255\346\263\225/10.\345\255\227\347\254\246\347\274\226\347\240\201\344\270\216\350\247\243\347\240\201/encode.md" @@ -11,8 +11,8 @@ if __name__ == '__main__': utf8_str = unicode_str.encode('utf-8') print(utf8_str) - unicode_str_again = utf8_str.decode('utf-8') - print(unicode_str_again) + # TODO(You):请在此实现代码 + unicode_str_again = ... assert unicode_str_again == unicode_str ``` @@ -36,22 +36,25 @@ if __name__ == '__main__': ## 答案 ```python - +unicode_str_again = utf8_str.decode('utf-8') ``` ## 选项 ### A -```python +```python +unicode_str_again = utf8_str.decode() ``` ### B -```python +```python +unicode_str_again = utf8_str.decode('unicode') ``` ### C -```python +```python +unicode_str_again = utf8_str.encode('unicode') ``` diff --git "a/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.json" "b/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.json" index 14ddb1a..110c58c 100644 --- "a/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.json" +++ "b/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.json" @@ -1,6 +1,8 @@ { - "source": "dataframe.py", - "depends": [], + "source": "dataframe.md", "exercise_id": 119, - "type": "code_options" + "depends": [], + "type": "code_options", + "author": "huanhuilong", + "notebook_enable": true } \ No newline at end of file diff --git "a/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.md" "b/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.md" new file mode 100644 index 0000000..3988f94 --- /dev/null +++ "b/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.md" @@ -0,0 +1,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 +``` diff --git "a/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.py" "b/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.py" index 86c65c7..9b9c8a2 100644 --- "a/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.py" +++ "b/data/3.python\351\253\230\351\230\266/2.\347\273\223\346\236\204\345\214\226\346\225\260\346\215\256\345\210\206\346\236\220\345\267\245\345\205\267Pandas/2.\346\225\260\346\215\256\347\273\223\346\236\204/dataframe.py" @@ -7,45 +7,53 @@ import pandas as pd import numpy as np -def create_dataframe(measure): - if measure == 1: - print('由数组list组成的字典创建dataframe:') - 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) - print(data_df) - - elif measure == 2: - print('由Series组成的字典创建dataframe:') - 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) - print(data_df) - elif measure == 3: - print('通过二维数组直接创建dataframe:') - data = np.random.randint(0, 10, size=9).reshape(3, 3) - data_df = pd.DataFrame( - data, columns=['cloumn_one', 'cloumn_two', 'cloumn_three']) - print(data_df) - elif measure == 4: - print('由字典组成的列表创建dataframe:') - data = [{'cloumn_one': 1, 'cloumn_two': 2, 'cloumn_three': 3}, - {'cloumn_one': 5, 'cloumn_two': 6, 'cloumn_three': 7}] - data_df = pd.DataFrame(data) - print(data_df) - else: - print('该参数不支持,可选参数为1,2,3,4') +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__': - create_dataframe(1) - create_dataframe(2) - create_dataframe(3) - create_dataframe(4) - create_dataframe(0) + 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) -- GitLab