Python 根据指定的字符串排列排列列在熊猫中的位置
我的dataframe在“A”和“B”之间具有类似的列名,如下所示:Python 根据指定的字符串排列排列列在熊猫中的位置,python,pandas,Python,Pandas,我的dataframe在“A”和“B”之间具有类似的列名,如下所示: df = pd.DataFrame({'A_Text1':1, 'A_Text2':2, 'A_Text3':3, 'B_Text1':4, 'B_Text2':5, 'B_Text3':6, 'A_Text4':7, 'B_Text4':8}) df = pd.DataFrame({'A_Text3':3, 'A_Text4':7, 'A_Text1':1, 'A_Text2':2, 'B_Text3':6, 'B_Te
df = pd.DataFrame({'A_Text1':1, 'A_Text2':2, 'A_Text3':3, 'B_Text1':4, 'B_Text2':5, 'B_Text3':6, 'A_Text4':7, 'B_Text4':8})
df = pd.DataFrame({'A_Text3':3, 'A_Text4':7, 'A_Text1':1, 'A_Text2':2, 'B_Text3':6, 'B_Text4':8, 'B_Text1':4, 'B_Text2':5})
当我从这里使用解决方案时,我会得到这样的结果,因为它按字母顺序排列:
df = pd.DataFrame({'A_Text1':1, 'A_Text2':2, 'A_Text3':3, 'A_Text4':7, 'B_Text1':4, 'B_Text2':5, 'B_Text3':6, 'B_Text4':8})
我真正需要的是能够按照我希望的方式排列列名,如下所示:
df = pd.DataFrame({'A_Text1':1, 'A_Text2':2, 'A_Text3':3, 'B_Text1':4, 'B_Text2':5, 'B_Text3':6, 'A_Text4':7, 'B_Text4':8})
df = pd.DataFrame({'A_Text3':3, 'A_Text4':7, 'A_Text1':1, 'A_Text2':2, 'B_Text3':6, 'B_Text4':8, 'B_Text1':4, 'B_Text2':5})
其中带有“\u Text”的列名的排列方式总是像我上面指定的那样(\u Text3、\u Text4、\u Text1、\u Text2),因为我有数百个具有类似“\u Text”名称的列。一个想法是使用helper
dictionary
为自定义顺序映射另一个dictionary以替换in重命名
,last用于按新列名称更改顺序:
order = {'Text3':'1', 'Text4':'2', 'Text1':'3', 'Text2':'4'}
d = df.columns.to_series().replace(order, regex=True).to_dict()
print (df.rename(columns=d).columns)
Index(['A_3', 'A_4', 'A_1', 'B_3', 'B_4', 'B_1', 'A_2', 'B_2'], dtype='object')
您还可以创建动态字典,如果列表中有10多列,还可以添加零以进行正确排序:
order = ['Text3', 'Text4', 'Text1', 'Text2']
order_d = {v: f'{k:03}' for k, v in enumerate(order, 1)}
print (order_d)
{'Text3': '001', 'Text4': '002', 'Text1': '003', 'Text2': '004'}
d = df.columns.to_series().replace(order_d, regex=True)
print (df.rename(columns=d).columns)
Index(['A_003', 'A_004', 'A_001',
'B_003', 'B_004', 'B_001', 'A_002', 'B_002'], dtype='object')
df = df.iloc[:, df.rename(columns=d).columns.argsort()]
print (df)
A_Text3 A_Text4 A_Text1 A_Text2 B_Text3 B_Text4 B_Text1 B_Text2
0 3 7 1 2 6 8 4 5
让我们尝试一下pd.category第一个想法是创建列的数据帧,并提取字母和数字表示 然后我们应用分类排序
s = df.columns.to_frame('')
s = s.join(s[0].str.extract("(\w)_\D+(\d)").rename(columns={0: "alpha", 1: "numeric"}))
print(s)
0 alpha numeric
0 A_Text1 A 1
1 A_Text2 A 2
2 A_Text3 A 3
3 B_Text1 B 1
4 B_Text2 B 2
5 B_Text3 B 3
6 A_Text4 A 4
7 B_Text4 B 4
#define your custom order.
order = [3,4,1,2]
s['numeric'] = pd.Categorical(s['numeric'].astype(int),order,ordered=True)
s_ordered = s.sort_values(['alpha','numeric'])
0 alpha numeric
2 A_Text3 A 3
6 A_Text4 A 4
0 A_Text1 A 1
1 A_Text2 A 2
5 B_Text3 B 3
7 B_Text4 B 4
3 B_Text1 B 1
4 B_Text2 B 2
然后重新分配到您的列
df.columns = s_ordered[0].tolist()
A_Text3 A_Text4 A_Text1 A_Text2 B_Text3 B_Text4 B_Text1 B_Text2
0 1 2 3 4 5 6 7 8
非常好的解决方案+1