# 创建数据

```python
import pandas as pd
import numpy as np
```

## 创建一个Series数据

```python
s = pd.Series([1,2,3,np.nan,5])
print(s)
'''
0    1.0
1    2.0
2    3.0
3    NaN
4    5.0
dtype: float64
'''
```

## 时间类型

```python
date = pd.date_range('20170101',periods=7)
print(date)
'''
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
               '2017-01-05', '2017-01-06', '2017-01-07'],
              dtype='datetime64[ns]', freq='D')
'''
```

## 表格类型，index是行的索引

```python
s=pd.DataFrame(np.random.rand(7,4),index=date,columns=['A','B','C','D'])
s

'''
    A    B    C    D
2017-01-01    0.246755    0.886655    0.024619    0.078501
2017-01-02    0.095470    0.012911    0.723686    0.217888
2017-01-03    0.059372    0.635223    0.073233    0.531136
2017-01-04    0.437067    0.454805    0.909538    0.603861
2017-01-05    0.501499    0.651960    0.022764    0.519166
2017-01-06    0.148038    0.564203    0.051323    0.001642
2017-01-07    0.375003    0.318459    0.376165    0.712314

'''
```

## 多类型表格

```python
# 要么一个长度，数据会被扩展到全部；要么等于最大长度（4）
df2 = pd.DataFrame({ 'A' : 1.,
                     'B' : pd.Timestamp('20170102'),
                     'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
                     'D' : np.array([3] * 4,dtype='int32'),
                     'E' : pd.Categorical(["test","train","test","train"]),
                     'F' : 'foo' })

print(df2)
'''
 A          B    C  D      E    F
0  1.0 2017-01-02  1.0  3   test  foo
1  1.0 2017-01-02  1.0  3  train  foo
2  1.0 2017-01-02  1.0  3   test  foo
3  1.0 2017-01-02  1.0  3  train  foo
'''
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/numpy-pandas-matplotlib/pandas/create-data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
