Pandas的基本用法(三)——设置数据

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要是关于pandas的一些基本用法。

#!/usr/bin/env python
# _*_ coding: utf-8 _*_

import pandas as pd
import numpy as np


# Test 1
# 定义数据
dates = pd.date_range('20170101', periods = 6)
print dates

df = pd.DataFrame(np.arange(24).reshape((6, 4)), index = dates, columns = ['A', 'B', 'C', 'D'])
print df

# Test 1 result
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
               '2017-01-05', '2017-01-06'],
              dtype='datetime64[ns]', freq='D')

             A   B   C   D
2017-01-01   0   1   2   3
2017-01-02   4   5   6   7
2017-01-03   8   9  10  11
2017-01-04  12  13  14  15
2017-01-05  16  17  18  19
2017-01-06  20  21  22  23

# Test 2
# 设置df[2,2]为100
df.iloc[2, 2] = 100
print df

# loc设置值
df.loc['20170102', 'C'] = 999
print df

# 根据条件设置值
df[df.A > 8] = 0
print df

# 根据ix设置值
df.ix[[0, 2],  ['A', 'C']] = 888
print df

# 限定设置区域
df.B[df.B == 0] = 6
print df 

# Test 2 result
             A   B    C   D
2017-01-01   0   1    2   3
2017-01-02   4   5    6   7
2017-01-03   8   9  100  11
2017-01-04  12  13   14  15
2017-01-05  16  17   18  19
2017-01-06  20  21   22  23

             A   B    C   D
2017-01-01   0   1    2   3
2017-01-02   4   5  999   7
2017-01-03   8   9  100  11
2017-01-04  12  13   14  15
2017-01-05  16  17   18  19
2017-01-06  20  21   22  23

            A  B    C   D
2017-01-01  0  1    2   3
2017-01-02  4  5  999   7
2017-01-03  8  9  100  11
2017-01-04  0  0    0   0
2017-01-05  0  0    0   0
2017-01-06  0  0    0   0

              A  B    C   D
2017-01-01  888  1  888   3
2017-01-02    4  5  999   7
2017-01-03  888  9  888  11
2017-01-04    0  0    0   0
2017-01-05    0  0    0   0
2017-01-06    0  0    0   0

              A  B    C   D
2017-01-01  888  1  888   3
2017-01-02    4  5  999   7
2017-01-03  888  9  888  11
2017-01-04    0  6    0   0
2017-01-05    0  6    0   0
2017-01-06    0  6    0   0

# Test 3
# 添加一列
df['F'] = None
print df

# 添加一列
df['E'] = pd.Series([1, 2, 3, 4, 5, 6], index = dates)
print df

# Test 3 result
              A  B    C   D     F
2017-01-01  888  1  888   3  None
2017-01-02    4  5  999   7  None
2017-01-03  888  9  888  11  None
2017-01-04    0  6    0   0  None
2017-01-05    0  6    0   0  None
2017-01-06    0  6    0   0  None

              A  B    C   D     F  E
2017-01-01  888  1  888   3  None  1
2017-01-02    4  5  999   7  None  2
2017-01-03  888  9  888  11  None  3
2017-01-04    0  6    0   0  None  4
2017-01-05    0  6    0   0  None  5
2017-01-06    0  6    0   0  None  6
    原文作者:SnailTyan
    原文地址: https://www.jianshu.com/p/dd7aefc8bad7
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞