构建3个dataframe,分别命名为df1、df2和df3,均为7行5列,数值为-10至10之间的随机整数,其中每个dataframe当中都有4个数是空值
时间: 2024-02-15 12:05:01 浏览: 110
pd.DataFrame统计各列数值多少的实例
好的,以下是Python代码实现:
```python
import pandas as pd
import numpy as np
# 设置随机种子,保证每次运行得到的随机数相同
np.random.seed(0)
# 构建df1
df1 = pd.DataFrame(np.random.randint(-10, 11, size=(7, 5)), columns=['col1', 'col2', 'col3', 'col4', 'col5'])
df1.iloc[2, 1] = np.nan
df1.iloc[0, 3] = np.nan
df1.iloc[4, 0] = np.nan
df1.iloc[5, 2] = np.nan
# 构建df2
df2 = pd.DataFrame(np.random.randint(-10, 11, size=(7, 5)), columns=['col1', 'col2', 'col3', 'col4', 'col5'])
df2.iloc[3, 2] = np.nan
df2.iloc[6, 4] = np.nan
df2.iloc[2, 0] = np.nan
df2.iloc[5, 1] = np.nan
# 构建df3
df3 = pd.DataFrame(np.random.randint(-10, 11, size=(7, 5)), columns=['col1', 'col2', 'col3', 'col4', 'col5'])
df3.iloc[1, 0] = np.nan
df3.iloc[4, 3] = np.nan
df3.iloc[6, 2] = np.nan
df3.iloc[2, 4] = np.nan
```
这样,我们就构建了三个7行5列的dataframe,数值为-10至10之间的随机整数,并且每个dataframe都有4个数是空值。
阅读全文