Pandas读取表格存入列表
时间: 2023-06-09 11:08:13 浏览: 61
可以使用 Pandas 的 read_excel() 或 read_csv() 方法来读取表格文件,并将表格数据存入一个列表中。例如,使用 read_excel() 方法读取 Excel 文件:
```
import pandas as pd
# 读取 Excel 文件
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
# 将表格数据存入列表
data = df.values.tolist()
```
同样,使用 read_csv() 方法读取 CSV 文件:
```
import pandas as pd
# 读取 CSV 文件
df = pd.read_csv('data.csv')
# 将表格数据存入列表
data = df.values.tolist()
```
注意,使用 read_excel() 方法需要安装 openpyxl 和 xlrd 库,而使用 read_csv() 方法则不需要。
相关问题
如何读取excl表格并存入空列表
您可以使用Python中的pandas库来读取excel表格并存入空列表。以下是示例代码:
```python
import pandas as pd
# 读取excel表格并获取第一个sheet
df = pd.read_excel('filename.xlsx', sheet_name=0)
# 将表格中的每一行存入一个空列表中
data = []
for row in df.iterrows():
data.append(list(row[1]))
print(data)
```
在这个例子中,我们首先使用pandas库的read_excel函数读取excel表格并获取第一个sheet。然后,我们使用iterrows函数遍历每一行并将其转换为列表,最终将每一行的列表存入一个空列表中。
python pandas将表格数据分别存入新表格sheet1和sheet2
假设你已经用 Pandas 读取了表格数据并存储在名为 `df` 的 DataFrame 中,可以按照以下步骤将数据分别存入新表格的 Sheet1 和 Sheet2 中:
```python
import pandas as pd
# 读取原始表格数据
df = pd.read_excel('原始表格.xlsx')
# 将数据按照条件分成两个 DataFrame
condition = df['条件列'] == '条件值'
df_sheet1 = df[condition]
df_sheet2 = df[~condition]
# 创建新表格并将两个 DataFrame 存储在不同的 Sheet 中
writer = pd.ExcelWriter('新表格.xlsx', engine='xlsxwriter')
df_sheet1.to_excel(writer, sheet_name='Sheet1')
df_sheet2.to_excel(writer, sheet_name='Sheet2')
writer.save()
```
其中,`df_sheet1` 和 `df_sheet2` 分别是按照条件分割后的两个 DataFrame,`writer` 是用于创建新表格的 ExcelWriter 对象,`to_excel()` 方法用于将 DataFrame 存储在指定的 Sheet 中,`writer.save()` 方法用于保存新表格。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)