python如何读取excel中某几行几列数据的应用实例
时间: 2023-03-07 22:07:15 浏览: 164
使用python读取excel中某几行几列数据的应用实例,可以使用Python的openpyxl库来实现。例如,通过以下代码可以获取excel中第2行第3列的数据: wb = openpyxl.load_workbook('test.xlsx')
ws = wb.active
data = ws.cell(row=2,column=3).value
相关问题
python读取excel中的某几列代码
可以使用Python中的第三方库pandas来读取Excel中的数据,以下是读取Excel中某几列数据的代码示例:
``` python
import pandas as pd
# 读取Excel文件
df = pd.read_excel('filename.xlsx')
# 读取指定的列
cols = ['column1', 'column2', 'column3']
new_df = df[cols]
# 打印读取的数据
print(new_df)
```
其中,`filename.xlsx`是Excel文件名,`column1`、`column2`、`column3`是需要读取的列名。读取后的数据保存在`new_df`变量中,可以根据需要进一步处理或输出。
使用python读取excel中的某几列数据并绘制正态分布直方图与核密度图
首先,需要安装以下库:pandas、matplotlib、seaborn、numpy
然后,可以按照以下步骤进行操作:
1. 导入所需的库
```python
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
```
2. 读取Excel文件中的数据
```python
df = pd.read_excel('data.xlsx', usecols=['列1', '列2', '列3'])
```
3. 绘制直方图
```python
plt.hist(df['列1'], bins=20, density=True, alpha=0.5, color='blue')
plt.hist(df['列2'], bins=20, density=True, alpha=0.5, color='green')
plt.hist(df['列3'], bins=20, density=True, alpha=0.5, color='red')
plt.xlabel('数据')
plt.ylabel('频率')
plt.title('正态分布直方图')
plt.show()
```
4. 绘制核密度图
```python
sns.kdeplot(df['列1'], shade=True, color='blue')
sns.kdeplot(df['列2'], shade=True, color='green')
sns.kdeplot(df['列3'], shade=True, color='red')
plt.xlabel('数据')
plt.ylabel('密度')
plt.title('核密度图')
plt.show()
```
完整代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
df = pd.read_excel('data.xlsx', usecols=['列1', '列2', '列3'])
plt.hist(df['列1'], bins=20, density=True, alpha=0.5, color='blue')
plt.hist(df['列2'], bins=20, density=True, alpha=0.5, color='green')
plt.hist(df['列3'], bins=20, density=True, alpha=0.5, color='red')
plt.xlabel('数据')
plt.ylabel('频率')
plt.title('正态分布直方图')
plt.show()
sns.kdeplot(df['列1'], shade=True, color='blue')
sns.kdeplot(df['列2'], shade=True, color='green')
sns.kdeplot(df['列3'], shade=True, color='red')
plt.xlabel('数据')
plt.ylabel('密度')
plt.title('核密度图')
plt.show()
```