-
Notifications
You must be signed in to change notification settings - Fork 2
/
3.6 dataframe_rename.py
30 lines (22 loc) · 1.03 KB
/
3.6 dataframe_rename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 重命名列
import pandas as pd
import collections
url = 'https://raw.githubusercontent.com/chrisalbon/simulated_datasets/master/titanic.csv'
dataframe = pd.read_csv(url)
result = dataframe.head(2)
print(result)
# Name PClass Age Sex Survived SexCode
# 1 Allison, Miss Helen Loraine 1st 2.0 female 0 1
# 0 Allen, Miss Elisabeth Walton 1st 29.0 female 1 1
# ---------------重命名列--------------------
result = dataframe.rename(columns={'PClass': 'Passenger Class'}).head(2)
print(result)
# Name Passenger Class ... Survived SexCode
# 0 Allen, Miss Elisabeth Walton 1st ... 1 1
# 1 Allison, Miss Helen Loraine 1st ... 0 1
# -----------创建字典-----
column_names = collections.defaultdict(str)
for name in dataframe.columns:
column_names[name]
print(column_names)
# defaultdict(<class 'str'>, {'Name': '', 'PClass': '', 'Age': '', 'Sex': '', 'Survived': '', 'SexCode': ''})