pandas的基本用法(三)——设置数据 | | pandas的基本用法(三)——设置数据 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 本文主要是关于pandas的一些基本用法。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115#!/usr/bin/env python# _*_ coding: utf-8 _*_import pandas as pdimport numpy as np# Test 1# 定义数据dates = pd.date_range('20170101', periods = 6)print datesdf = pd.DataFrame(np.arange(24).reshape((6, 4)), index = dates, columns = ['A', 'B', 'C', 'D'])print df# Test 1 resultDatetimeIndex(['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 D2017-01-01 0 1 2 32017-01-02 4 5 6 72017-01-03 8 9 10 112017-01-04 12 13 14 152017-01-05 16 17 18 192017-01-06 20 21 22 23# Test 2# 设置df[2,2]为100df.iloc[2, 2] = 100print df# loc设置值df.loc['20170102', 'C'] = 999print df# 根据条件设置值df[df.A > 8] = 0print df# 根据ix设置值df.ix[[0, 2], ['A', 'C']] = 888print df# 限定设置区域df.B[df.B == 0] = 6print df # Test 2 result A B C D2017-01-01 0 1 2 32017-01-02 4 5 6 72017-01-03 8 9 100 112017-01-04 12 13 14 152017-01-05 16 17 18 192017-01-06 20 21 22 23 A B C D2017-01-01 0 1 2 32017-01-02 4 5 999 72017-01-03 8 9 100 112017-01-04 12 13 14 152017-01-05 16 17 18 192017-01-06 20 21 22 23 A B C D2017-01-01 0 1 2 32017-01-02 4 5 999 72017-01-03 8 9 100 112017-01-04 0 0 0 02017-01-05 0 0 0 02017-01-06 0 0 0 0 A B C D2017-01-01 888 1 888 32017-01-02 4 5 999 72017-01-03 888 9 888 112017-01-04 0 0 0 02017-01-05 0 0 0 02017-01-06 0 0 0 0 A B C D2017-01-01 888 1 888 32017-01-02 4 5 999 72017-01-03 888 9 888 112017-01-04 0 6 0 02017-01-05 0 6 0 02017-01-06 0 6 0 0# Test 3# 添加一列df['F'] = Noneprint df# 添加一列df['E'] = pd.Series([1, 2, 3, 4, 5, 6], index = dates)print df# Test 3 result A B C D F2017-01-01 888 1 888 3 None2017-01-02 4 5 999 7 None2017-01-03 888 9 888 11 None2017-01-04 0 6 0 0 None2017-01-05 0 6 0 0 None2017-01-06 0 6 0 0 None A B C D F E2017-01-01 888 1 888 3 None 12017-01-02 4 5 999 7 None 22017-01-03 888 9 888 11 None 32017-01-04 0 6 0 0 None 42017-01-05 0 6 0 0 None 52017-01-06 0 6 0 0 None 6 参考资料 https://www.youtube.com/user/MorvanZhou 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏