[英]Sort pandas dataframe headers with two conditions
我有一個問題:我有一個大的 dataframe 超過 1000 列。
例如以下列的消息:2019 年材料成本、2019 年人工成本、2019 年間接成本、2020 年材料成本、2020 年人工成本、2020 年間接成本、...2035
df = pd.DataFrame({'2019 Material cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2019 Overhead cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2019 Labor cost': [11, 8, 10, 6, 6, 5, 9, 12],
'2020 Material cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2020 Overhead cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2020 Labor cost': [11, 8, 10, 6, 6, 5, 9, 12],
'2021 Material cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2021 Overhead cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2021 Labor cost': [11, 8, 10, 6, 6, 5, 9, 12],
})
我想將所有標題排序為以下內容:
2019 年材料成本、2020 年材料成本、2021 年材料成本、...、2019 年人工成本、2020 年人工成本、2021 年人工成本、... ,2019 年間接成本、2020 年間接成本、2021 年間接成本
df = pd.DataFrame({'2019 Material cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2020 Material cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2021 Material cost': [11, 8, 10, 6, 6, 5, 9, 12],
'2019 Overhead cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2020 Overhead cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2021 Overhead cost': [11, 8, 10, 6, 6, 5, 9, 12],
'2019 Labor cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2020 Labor cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2021 Labor cost': [11, 8, 10, 6, 6, 5, 9, 12],
})
所以我想要一個成本類別,並按以下順序對類別的年份進行排序,然后是下一個類別。
這里有什么幫助嗎? 提前致謝
創建兩個列表,一個包含成本,一個包含年份。 使用這些列表,您可以創建另一個包含所有列名的列表(按順序)。
costs = list(df.columns.str[5:].unique())
years = list(range(2019, 2036))
columns = [str(year) + ' ' + cost for year in years for cost in costs]
df = df.reindex(columns=columns)
例如:
df = pd.DataFrame(np.random.random((10, 10)), columns = ['1 a', '2 a', '3 a', '4 a', '5 a', '1 b', '2 b', '3 b', '4 b', '5 b'])
costs = ['a', 'b']
years = [1, 2, 3, 4, 5]
columns = [str(year) + ' ' + cost for year in years for cost in costs]
df.reindex(columns=columns).columns
退貨
Index(['1 a', '1 b', '2 a', '2 b', '3 a', '3 b', '4 a', '4 b', '5 a', '5 b'], dtype='object')
@Chris 給出輸入:
df = pd.DataFrame({'2019 Material cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2019 Overhead cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2019 Labor cost': [11, 8, 10, 6, 6, 5, 9, 12],
'2020 Material cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2020 Overhead cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2020 Labor cost': [11, 8, 10, 6, 6, 5, 9, 12],
'2021 Material cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2021 Overhead cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2021 Labor cost': [11, 8, 10, 6, 6, 5, 9, 12],
})
我想把它作為 output (按類別排序並按年升序排列):
df = pd.DataFrame({'2019 Material cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2020 Material cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2021 Material cost': [11, 8, 10, 6, 6, 5, 9, 12],
'2019 Overhead cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2020 Overhead cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2021 Overhead cost': [11, 8, 10, 6, 6, 5, 9, 12],
'2019 Labor cost': [25, 12, 15, 14, 19, 23, 25, 29],
'2020 Labor cost ': [5, 7, 7, 9, 12, 9, 9, 4],
'2021 Labor cost': [11, 8, 10, 6, 6, 5, 9, 12],
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.