“使用Pandas添加List”的版本间的差异
来自YTYZX有图有真相的百科
(e) (2个标签:移动版编辑,移动网页编辑) |
(撤销51.142.191.92(讨论)的版本8745) |
||
(未显示1个用户的7个中间版本) | |||
第1行: | 第1行: | ||
− | + | 在Python中使用以下代码将循环生成的列表依次添加为DataFrame的一行,并导出到Excel表格里面。 | |
+ | <syntaxhighlight lang="python"> | ||
+ | import pandas as pd | ||
+ | df = pd.DataFrame() | ||
+ | for i in range(10): | ||
+ | j = i**2 | ||
+ | list1 = [[i, j]] | ||
+ | df = df.append(list1, ignore_index=True) | ||
+ | df.columns = ['自然数', '平方值'] # 给DataFrame添加columns | ||
+ | df.to_excel(r'C:\GITPythonLesson\num.xlsx',sheet_name='Sheet1',engine='openpyxl') | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 输出结果为下图: | ||
+ | [[File:PandasAppendList1.png]] |
2023年2月19日 (日) 08:42的最新版本
在Python中使用以下代码将循环生成的列表依次添加为DataFrame的一行,并导出到Excel表格里面。 <syntaxhighlight lang="python"> import pandas as pd df = pd.DataFrame() for i in range(10): j = i**2 list1 = i, j df = df.append(list1, ignore_index=True) df.columns = ['自然数', '平方值'] # 给DataFrame添加columns df.to_excel(r'C:\GITPythonLesson\num.xlsx',sheet_name='Sheet1',engine='openpyxl') </syntaxhighlight>
输出结果为下图: