簡體   English   中英

Pandas - 將元組格式的標題名稱轉換為字符串

[英]Pandas - convert header name in tuple format to a string

在計算組內的百分位數后,標題名稱采用元組格式,如[('A', 0.5), ('A',0.9)...('Z',0.9)]

所需的輸出應該是:

['P50 A', 'P90 A', ...'P90 Z']

基本上,我想將小數乘以 100 以獲得百分比並將其移動到前面,在每個字段中附加一個字母“P”。

我覺得我應該像這里建議的那樣使用 map 或 join: How to change the columns name from a tuple to string?

但不確定如何處理細節。

這也有效:

original_names = [('A', 0.5), ('A',0.9),('Z',0.9)]
new_names = ['P'+str(int(100*y)) + ' ' + x for x,y in original_names]

結果:['P50 A'、'P90 A'、'P90 Z']

當前列名:

col_name = [('A', 0.5), ('A',0.9),('Z',0.9)]

所需的列名:

desired_col_named = [f'P{x[1]*100} {x[0]}' for x in col_name]

假設您有支持 f 字符串的 python 3.6。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM