码迷,mamicode.com
首页 > 编程语言 > 详细

Python——rename更改Series和DataFrame的标签名(即列标签)

时间:2017-03-30 18:35:59      阅读:6856      评论:0      收藏:0      [点我收藏+]

标签:doc   table   das   callable   ges   rename   error:   html   typeerror   

转载:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename.html

>>> s = pd.Series([1, 2, 3])
>>> s
0    1
1    2
2    3
dtype: int64
>>> s.rename("my_name") # scalar, changes Series.name
0    1
1    2
2    3
Name: my_name, dtype: int64
>>> s.rename(lambda x: x ** 2)  # function, changes labels
0    1
1    2
4    3
dtype: int64
>>> s.rename({1: 3, 2: 5})  # mapping, changes labels
0    1
3    2
5    3
dtype: int64
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.rename(2)
...
TypeError: ‘int‘ object is not callable
>>> df.rename(index=str, columns={"A": "a", "B": "c"})
   a  c
0  1  4
1  2  5
2  3  6
>>> df.rename(index=str, columns={"A": "a", "C": "c"})
   a  B
0  1  4
1  2  5
2  3  6

 

Python——rename更改Series和DataFrame的标签名(即列标签)

标签:doc   table   das   callable   ges   rename   error:   html   typeerror   

原文地址:http://www.cnblogs.com/ming-zi/p/6647589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!