簡體   English   中英

將單列中的數據與python DataFrame中的右側對齊

[英]Aligning data in a single column to the right in a python DataFrame

我目前在 Python 中有一個數據框,現在我試圖將一列向右對齊,因為當我將它導出到 excel 時,它當前正在向左對齊。

姓名 區域 位置類型
紅色的 1234
綠色 4567 30.23英里每小時
藍色 8901

在這種情況下,我當前嘗試移動的列是位置類型,它同時包含數字、字母和一些空白行。

我嘗試使用這行代碼:

df = df.style.set_properties(subset=["Location Type"], **{'text-align': 'right'})

我嘗試在其他列上對其進行測試,以查看該列是否有問題,但我在這兩列中都收到了此錯誤。

ValueError: style is not supported for non-unique indices.

有人能告訴我我做錯了什么嗎?

先感謝您

我無法讓它向右對齊,所以我注意到 excel 將數字向右對齊。

我刪除了所有字符串字符,並使用以下兩行將列轉換為浮動。

df['Location Type'] = df['Location Type'].str.extract('(\d+\.*\d*)', expand=False)
df['Location Type'] = df['Location Type'].astype(float)

暫無
暫無

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

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