2017-07-27 8 views
1

私はExcelシートに書き込むパンダのデータフレームを持っています。次に、そのDataFrameのヒートマップを表示するようにフォーマットします。xlsxwriter on python、 '3_color_scale'でconditional_format関数を使用

私は、これについての最良の方法は、{'type': '3_color_scale'}を引数としてworksheet.conditional_format()関数を使用することです。

しかし、私はこのが整数を保持するセルの色だけに気づいた。それはそれにフロートを持っているものは何も着色されません。誰もこの問題を解決する方法を知っていますか?

ここにコードのサンプルを示します。

writer = pd.ExcelWriter(file_name, engine='xlsxwriter') 
df.to_excel(writer, sheet_name=selected_factor, startrow=row_location, startcol=col_location) 
worksheet = writer.sheets['Sheet 1 testing'] 
worksheet.conditional_format('B8:E17',{'type': '3_color_scale'}) 

は私がPython 2.7pandas version 0.15.2を使用しています。それは私に迷惑を与えていた私はなぜ考え出し

EDITは、数字ではない山車としてスプレッドシートに文字列として保存されていました。

答えて

1

次の例を実行すると、この問題は発生しません。これは、整数を強調し、山車:

import pandas as pd 

# Create a Pandas dataframe from some data. 
df = pd.DataFrame({'Data': [10, 20.5, 30, 40, 50.7, 62, 70]}) 

# Create a Pandas Excel writer using XlsxWriter as the engine. 
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter') 

# Convert the dataframe to an XlsxWriter Excel object. 
df.to_excel(writer, sheet_name='Sheet1') 

# Get the xlsxwriter workbook and worksheet objects. 
workbook = writer.book 
worksheet = writer.sheets['Sheet1'] 

# Apply a conditional format to the cell range. 
worksheet.conditional_format('B2:B8', {'type': '3_color_scale'}) 

# Close the Pandas Excel writer and output the Excel file. 
writer.save() 

出力:それは私のために働いていなかった理由を、私は考え出し

enter image description here

+0

@jmcnamraは、数字が浮いていない文字列として格納されていました! –

関連する問題