多次为 selecttemp 指定了列 bstkd_Pandas核心能力4:列

Pandas核心能力4:列

In [1]:

import pandas as pddf = pd.read_csv('pandas_data.csv', encoding = 'gbk')print (df.head()) 日期 时间 日期时间 字符串 str int 0 2018/12/30 17:30 2018/12/31 17:30 这是中文 python 309616 1 2018/12/31 18:30 2019/1/1 17:30 中文 this is a str 465817 2 2019/1/1 19:30 2019/1/2 17:30 中 文 this 3618 3 2019/1/2 20:30 2019/1/3 17:30 中 文 is 6819 4 2019/1/3 21:30 2019/1/4 17:30 中文 a string 4861620  float ABC 甲乙丙 123 A乙3 手机号 身份证号 科学计数 0 3.141593 A 丙 2 c丙9 15666556600 1103261966020355961 1.103260e+18 1 4.141593 B 甲 3 b丙10 15666556601 6103261987020353021 1.103260e+18 2 5.141593 C 乙 4 c丙11 15666556602 1103261966050354432 1.103260e+18 3 6.141593 E 甲 5 c乙12 15666556603 1103261986050355601 1.103260e+18 4 7.141593 F 戊 6 a丙13 15666556602 11032619660203501X 1.103260e+17 

1. 查看列

1.1 查看列名

In [2]:

df.columns

Out[2]:

Index(['日期', '时间', '日期时间', '字符串', 'str', 'int', 'float', 'ABC', '甲乙丙', '123', 'A乙3', '手机号', '身份证号', '科学计数'], dtype='object')

1.2 查看列数据类型

In [3]:

df.dtypes

Out[3]:

日期 object时间 object日期时间 object字符串 objectstr objectint int64float float64ABC object甲乙丙 object123 int64A乙3 object手机号 int64身份证号 object科学计数 float64dtype: object

1.3 转换列数据类型

In [4]:

df['日期时间'] = pd.to_datetime(df['日期时间']) # 将“日期时间”列转换为datatime类型

In [5]:

df.dtypes

Out[5]:

日期 object时间 object日期时间 datetime64[ns]字符串 objectstr objectint int64float float64ABC object甲乙丙 object123 int64A乙3 object手机号 int64身份证号 object科学计数 float64dtype: object

2. 新建列

2.1 在列尾添加一列

In [6]:

df['新建空列'] = None # 在列尾新建一个名为“新建列”的空列

In [7]:

df.head()

Out[7]:

日期时间日期时间字符串strintfloatABC甲乙丙123A乙3手机号身份证号科学计数新建空列02018/12/3017:302018-12-31 17:30:00这是中文python3096163.141593A丙2c丙91566655660011032619660203559611.103260e+18None12018/12/3118:302019-01-01 17:30:00中文this is a str4658174.141593B甲3b丙101566655660161032619870203530211.103260e+18None22019/1/119:302019-01-02 17:30:00中 文this36185.141593C乙4c丙111566655660211032619660503544321.103260e+18None32019/1/220:302019-01-03 17:30:00中 文is68196.141593E甲5c乙121566655660311032619860503556011.103260e+18None42019/1/321:302019-01-04 17:30:00中文a string48616207.141593F戊6a丙131566655660211032619660203501X1.103260e+17None

In [8]:

df['新建99'] = [99] * 13 # 新建一外列名为“新建99”,内容全是99的列

In [9]:

df.head()

Out[9]:

日期时间日期时间字符串strintfloatABC甲乙丙123A乙3手机号身份证号科学计数新建空列新建9902018/12/3017:302018-12-31 17:30:00这是中文python3096163.141593A丙2c丙91566655660011032619660203559611.103260e+18None9912018/12/3118:302019-01-01 17:30:00中文this is a str4658174.141593B甲3b丙101566655660161032619870203530211.103260e+18None9922019/1/119:302019-01-02 17:30:00中 文this36185.141593C乙4c丙111566655660211032619660503544321.103260e+18None9932019/1/220:302019-01-03 17:30:00中 文is68196.141593E甲5c乙121566655660311032619860503556011.103260e+18None9942019/1/321:302019-01-04 17:30:00中文a string48616207.141593F戊6a丙131566655660211032619660203501X1.103260e+17None99

2.2 在指定位置插入列 insert()

In [10]:

df.insert(0,'指定0', [0] * 13) # 在指定位置(0即第一列),插入列名为“指定首”,值全为0的列

In [11]:

df.head()

Out[11]:

指定0日期时间日期时间字符串strintfloatABC甲乙丙123A乙3手机号身份证号科学计数新建空列新建99002018/12/3017:302018-12-31 17:30:00这是中文python3096163.141593A丙2c丙91566655660011032619660203559611.103260e+18None99102018/12/3118:302019-01-01 17:30:00中文this is a str4658174.141593B甲3b丙101566655660161032619870203530211.103260e+18None99202019/1/119:302019-01-02 17:30:00中 文this36185.141593C乙4c丙111566655660211032619660503544321.103260e+18None99302019/1/220:302019-01-03 17:30:00中 文is68196.141593E甲5c乙121566655660311032619860503556011.103260e+18None99402019/1/321:302019-01-04 17:30:00中文a string48616207.141593F戊6a丙131566655660211032619660203501X1.103260e+17None99

3. 删除列 drop()

In [12]:

df.drop(columns = ['指定0', '新建空列', '新建99'], inplace = True) # 删除列名为[]内的列,inplace = True直接操作原数据

In [13]:

df.head() # 直接操作原数据后,指定的列全部被删除,不可恢复

Out[13]:

日期时间日期时间字符串strintfloatABC甲乙丙123A乙3手机号身份证号科学计数02018/12/3017:302018-12-31 17:30:00这是中文python3096163.141593A丙2c丙91566655660011032619660203559611.103260e+1812018/12/3118:302019-01-01 17:30:00中文this is a str4658174.141593B甲3b丙101566655660161032619870203530211.103260e+1822019/1/119:302019-01-02 17:30:00中 文this36185.141593C乙4c丙111566655660211032619660503544321.103260e+1832019/1/220:302019-01-03 17:30:00中 文is68196.141593E甲5c乙121566655660311032619860503556011.103260e+1842019/1/321:302019-01-04 17:30:00中文a string48616207.141593F戊6a丙131566655660211032619660203501X1.103260e+17

4. 修改列

In [14]:

df['int'] = [99] * len(df.index)df.head()

Out[14]:

日期时间日期时间字符串strintfloatABC甲乙丙123A乙3手机号身份证号科学计数02018/12/3017:302018-12-31 17:30:00这是中文python993.141593A丙2c丙91566655660011032619660203559611.103260e+1812018/12/3118:302019-01-01 17:30:00中文this is a str994.141593B甲3b丙101566655660161032619870203530211.103260e+1822019/1/119:302019-01-02 17:30:00中 文this995.141593C乙4c丙111566655660211032619660503544321.103260e+1832019/1/220:302019-01-03 17:30:00中 文is996.141593E甲5c乙121566655660311032619860503556011.103260e+1842019/1/321:302019-01-04 17:30:00中文a string997.141593F戊6a丙131566655660211032619660203501X1.103260e+17

5. 拆分列

In [15]:

df2 = df['日期'].str.split('/', expand = True)

In [16]:

df2.columns = list('年月日')df2.head()

Out[16]:

年月日020181230120181231220191132019124201913

6. 合并列

In [17]:

df2['年月日'] = df2['年'] + '-' + df2['月'] + '-' + df2['日'] # 将年月日三列合并为“年月日”一列,中间以“-”分隔df2.head()

Out[17]:

年月日年月日0201812302018-12-301201812312018-12-3122019112019-1-132019122019-1-242019132019-1-3

7. DadaFrame合并(添加多列)

In [18]:

df3 = df.join(df2) # 将df2添加到df列尾,形成一个新的DataFrame,df3df3.head()

Out[18]:

日期时间日期时间字符串strintfloatABC甲乙丙123A乙3手机号身份证号科学计数年月日年月日02018/12/3017:302018-12-31 17:30:00这是中文python993.141593A丙2c丙91566655660011032619660203559611.103260e+18201812302018-12-3012018/12/3118:302019-01-01 17:30:00中文this is a str994.141593B甲3b丙101566655660161032619870203530211.103260e+18201812312018-12-3122019/1/119:302019-01-02 17:30:00中 文this995.141593C乙4c丙111566655660211032619660503544321.103260e+182019112019-1-132019/1/220:302019-01-03 17:30:00中 文is996.141593E甲5c乙121566655660311032619860503556011.103260e+182019122019-1-242019/1/321:302019-01-04 17:30:00中文a string997.141593F戊6a丙131566655660211032619660203501X1.103260e+172019132019-1-3

951ec920b9f0103553845b60118b863b.png

pandas 列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值