Python Pandas 數據拼接/排序/重置
Pandas 數據拼接/排序/重置
一、數據拼接
準備數據:
import pandas as pd
d_1 = {'name' : pd.Series(['a', 'b', 'c', 'd'], index=[0,1,2,3]),
'attri_1' : pd.Series([1.1, 2.2, 3.3, 4.4,5.5], index=[0,1,2,3,4]),
'attri_2' : pd.Series([1, 2, 3, 4, 5], index=[0,1,2,3,4])
}
d_2 = {'name' : pd.Series(['aa', 'bb', 'cc', 'dd','ee'], index=[0,1,2,3,4]),
'attri_1' : pd.Series([2.1, 3.2, 4.3, 5.4, 6.5], index=[0,1,2,3,4]),
'attri_2' : pd.Series([1, 2, 3, 4, 5], index=[0,1,2,3,4])
}
df_1 = pd.DataFrame(d_1)
df_2 = pd.DataFrame(d_2)
print(df_1)
print(df_2)
運行結果:
name attri_1 attri_2
0 a 1.1 1
1 b 2.2 2
2 c 3.3 3
3 d 4.4 4
4 NaN 5.5 5
name attri_1 attri_2
0 aa 2.1 1
1 bb 3.2 2
2 cc 4.3 3
3 dd 5.4 4
4 ee 6.5 5
1.1 行拼接(縱向,第 0 維)pd.concat([df1,df2])
df_3 = pd.concat([df_1,df_2])
print(df_3)
運行結果:
name attri_1 attri_2
0 a 1.1 1
1 b 2.2 2
2 c 3.3 3
3 d 4.4 4
4 NaN 5.5 5
0 aa 2.1 1
1 bb 3.2 2
2 cc 4.3 3
3 dd 5.4 4
4 ee 6.5 5
注意事項:
- 這裡的兩組數據列標籤需要一致
- 拼接後的 index 還是保留原先的
1.2 列拼接(橫向,第 1 維)pd.concat([df1,df2], axis=1)
df_4 = pd.concat([df_1,df_2], axis=1)
print(df_4)
運行結果:
name attri_1 attri_2 name attri_1 attri_2
0 a 1.1 1 aa 2.1 1
1 b 2.2 2 bb 3.2 2
2 c 3.3 3 cc 4.3 3
3 d 4.4 4 dd 5.4 4
4 NaN 5.5 5 ee 6.5 5
二、排序
準備數據:
d = {'name' : pd.Series(['a', 'b', 'c', 'd'], index=[0,1,2,3]),
'attri_1' : pd.Series([5.1, 4.2, 2.3, 1.4,3.5], index=[0,1,2,3,4]),
'attri_2' : pd.Series([1, 2, 3, 4, 5], index=[0,1,2,3,4])
}
df = pd.DataFrame(d)
print(df)
運行結果:
name attri_1 attri_2
0 a 5.1 1
1 b 4.2 2
2 c 2.3 3
3 d 1.4 4
4 NaN 3.5 5
2.1 從小到大排序(默認)df.sort_values()
print(df.sort_values('attri_1')) # 排序後,index 也會相應變化
運行結果:
name attri_1 attri_2
3 d 1.4 4
2 c 2.3 3
4 NaN 3.5 5
1 b 4.2 2
0 a 5.1 1
2.2 從大到小排序(逆序)df.sort_values(ascending =False)
print(df.sort_values('attri_1',ascending =False))
print(df) # 這裡可以看到,排序不會直接作用於原數據,所以這裡列印出來的還是原來的形式
運行結果:
name attri_1 attri_2
0 a 5.1 1
1 b 4.2 2
4 NaN 3.5 5
2 c 2.3 3
3 d 1.4 4
name attri_1 attri_2
0 a 5.1 1
1 b 4.2 2
2 c 2.3 3
3 d 1.4 4
4 NaN 3.5 5
2.3 對 index 進行排序 df.sort_index()
df_5 = df.sort_values('attri_1')
print(df_5)
print(df_5.sort_index())
運行結果:
name attri_1 attri_2
3 d 1.4 4
2 c 2.3 3
4 NaN 3.5 5
1 b 4.2 2
0 a 5.1 1
name attri_1 attri_2
0 a 5.1 1
1 b 4.2 2
2 c 2.3 3
3 d 1.4 4
4 NaN 3.5 5
2.4 對 index 進行重置(變成默認的 0~ n)df.reset_index()
d = {
'attri_1' : pd.Series([5.1, 4.2, 2.3, 1.4,3.5], index=['a', 'b', 'c', 'd', 'e'])
}
df = pd.DataFrame(d)
print(df)
print(df.reset_index()) # 這裡重置了 index ,把原來的 index 變為列,標籤為 'index'
運行結果:
attri_1
a 5.1
b 4.2
c 2.3
d 1.4
e 3.5
index attri_1
0 a 5.1
1 b 4.2
2 c 2.3
3 d 1.4
4 e 3.5
三、對列標籤重命名 df.rename()
d = {
'attri_1' : pd.Series([5.1, 4.2, 2.3, 1.4,3.5], index=['a', 'b', 'c', 'd', 'e'])
}
df = pd.DataFrame(d)
print(df)
print(df.rename(columns={'attri_1':'new'}))
運行結果:
attri_1
a 5.1
b 4.2
c 2.3
d 1.4
e 3.5
new
a 5.1
b 4.2
c 2.3
d 1.4
e 3.5
