在Redshift中将文本转换为浮点数,输出相同。

huangapple go评论89阅读模式
英文:

Convert text to float in redshift with same output

问题

我返回的只是翻译好的:

我的视图返回文本字段输出,因为我用'%'符号连接我的字段。当我使用to_excel将这些数据推送到Excel时,它插入的是文本而不是数字。有没有办法解决这个问题,使我的Excel表格中显示数字而不是文本?

Redshift 代码:

select concat(((("replace"((myfield)::text, '%'::text, ''::text))::numeric(10,2))::character varying)::text, '%'::text) AS myfield_formatted
from myview

Python 代码将数据推送到Excel:

 df3 = df3.apply(pd.to_numeric, errors='ignore')
 df3.to_excel(writer, sheet_name=comp,startrow=0, startcol= 0, index=False, float_format='%.2f')

我应该在Redshift 层次还是Pandas 数据框架层次解决这个问题?

英文:

My view returns text field output since I concatenate my field with '%' symbol. When I push this data into excel using to_excel, it inserts as text and not numbers. Is there a way to fix this problem so that my excel sheets has numbers instead of text?

redshift code:

select concat(((("replace"((myfield)::text, '%'::text, ''::text))::numeric(10,2))::character varying)::text, '%'::text) AS myfield_formatted
from myview

Python code to push data into excel:

 df3 = df3.apply(pd.to_numeric, errors='ignore')
 df3.to_excel(writer, sheet_name=comp,startrow=0, startcol= 0, index=False, float_format='%.2f')

Should i fix thsi problem at redshift level or pandas dataframes level?

答案1

得分: 3

以下是翻译的部分:

Is there a way to fix this problem so that my excel sheets has numbers instead of text?

有没有办法解决这个问题,使我的Excel表格中有数字而不是文本?

The way to do that is to convert the text to numbers in pandas prior to exporting the dataframe to Excel. It looks like you are trying to do that but I think the % that is added to the strings is preventing a numeric conversion.

要做到这一点,需要在将数据框导出到Excel之前,在pandas中将文本转换为数字。看起来你正在尝试这样做,但我认为添加到字符串的百分比符号“%”会阻止数字转换。

Here is an example of converting some string data to percentages using the following steps:

以下是将一些字符串数据转换为百分比的示例,使用以下步骤:

  1. Convert the string to a number
    1. 将字符串转换为数字
  2. Divide by 100 to get a percentage
    2. 除以100以获得百分比
  3. Format the number with an Excel percentage format
    3. 使用Excel百分比格式格式化数字

(代码部分不翻译)

英文:

> Is there a way to fix this problem so that my excel sheets has numbers instead of text?

The way to do that is to convert the text to numbers in pandas prior to exporting the dataframe to Excel. It looks like you are trying to do that but I think the % that is added to the strings is preventing a numeric conversion.

Here is an example of converting some string data to percentages using the following steps:

  1. Convert the string to a number
  2. Divide by 100 to get a percentage
  3. Format the number with an Excel percentage format
import pandas as pd


# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Names': ['Anna', 'Arek', 'Arun'],
                   'Grade': ['100',  '70',   '85']})

# Convert the percentage strings to percentage numbers.
# df['Grade'] = df['Grade'].str.replace('%', '') # Add this if required.
df['Grade'] = df['Grade'].astype(float)
df['Grade'] = df['Grade'].div(100)

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_percent.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']

# Add a percent number format.
percent_format = workbook.add_format({'num_format': '0%'})

# Apply the number format to Grade column.
worksheet.set_column(2, 2, None, percent_format)

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

Output:

在Redshift中将文本转换为浮点数,输出相同。

答案2

得分: 2

你能尝试在XlsxWriter层面设置列格式吗?

英文:

Could you try to set the column format at the XlsxWriter level?

huangapple
  • 本文由 发表于 2023年3月3日 20:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定