python pandas
1 创建excel
1.1 创建空excel
1.2 创建带数据的excel
- 会在最左边一列显示0 1 2,是dataFrame的索引
import pandas as pd
df = pd.DataFrame({"id": [11, 22, 33], "name": ["xiaobai", "xiaohong", "xiaohei"]})
df.to_excel("output.xlsx")
点击查看生成的excel
| id | name | |
|---|---|---|
| 0 | 11 | xiaohei |
| 1 | 22 | xiaohong |
| 2 | 33 | xiaobai |
- 将id 设置为df的索引, 这样生成的excel 不会在前面用自动生成0 1 2..
2 读取excel
| id | name |
|---|---|
| 11 | xiaohei |
| 22 | xiaohong |
| 33 | xiaobai |
| 44 | xiaohuang |
| 55 | xiaolan |
| 66 | xiaozi |
user = pd.read_excel(file)
user.columns = ["user_id", "user_name"]
# inplace=True 表示直接修改, 否则你需要 user=user.set_index("user_id")
user.set_index("user_id",inplace=True) # 这样就不会输出 默认的索引列, 用user_id作为索引了.
user.to_excel("output5.xlsx")
点击查看生成的excel
| user_id | user_name |
|---|---|
| 11 | xiaohei |
| 22 | xiaohong |
| 33 | xiaobai |
| 44 | xiaohuang |
| 55 | xiaolan |
| 66 | xiaozi |