【发布时间】:2017-01-29 05:10:46
【问题描述】:
要求:
DataFrame 中的一个特定列是“混合”类型。它可以具有 "123456" 或 "ABC12345" 之类的值。
正在使用 xlsxwriter 将该数据框写入 Excel。
对于像"123456" 这样的值,Pandas 将其转换为123456.0(使其看起来像一个浮点数)
我们需要将它作为 123456(即 + 整数)放入 xlsx 中,以防值是全数字。
努力:
代码片段如下所示
import pandas as pd
import numpy as np
import xlsxwriter
import os
import datetime
import sys
excel_name = str(input("Please Enter Spreadsheet Name :\n").strip())
print("excel entered : " , excel_name)
df_header = ['DisplayName','StoreLanguage','Territory','WorkType','EntryType','TitleInternalAlias',
'TitleDisplayUnlimited','LocalizationType','LicenseType','LicenseRightsDescription',
'FormatProfile','Start','End','PriceType','PriceValue','SRP','Description',
'OtherTerms','OtherInstructions','ContentID','ProductID','EncodeID','AvailID',
'Metadata', 'AltID', 'SuppressionLiftDate','SpecialPreOrderFulfillDate','ReleaseYear','ReleaseHistoryOriginal','ReleaseHistoryPhysicalHV',
'ExceptionFlag','RatingSystem','RatingValue','RatingReason','RentalDuration','WatchDuration','CaptionIncluded','CaptionExemption','Any','ContractID',
'ServiceProvider','TotalRunTime','HoldbackLanguage','HoldbackExclusionLanguage']
first_pass_drop_duplicate = df_m_d.drop_duplicates(['StoreLanguage','Territory','TitleInternalAlias','LocalizationType','LicenseType',
'LicenseRightsDescription','FormatProfile','Start','End','PriceType','PriceValue','ContentID','ProductID',
'AltID','ReleaseHistoryPhysicalHV','RatingSystem','RatingValue','CaptionIncluded'], keep=False)
# We need to keep integer AltID as is
first_pass_drop_duplicate.loc[first_pass_drop_duplicate['AltID']] = first_pass_drop_duplicate['AltID'].apply(lambda x : str(int(x)) if str(x).isdigit() == True else x)
我试过了:
1. using `dataframe.astype(int).astype(str)` # works as long as value is not alphanumeric
2.importing re and using pure python `re.compile()` and `replace()` -- does not work
3.reading DF row by row in a for loop !!! Kills the machine as dataframe can have 300k+ records
每次都会出错:
raise KeyError('%s not in index' % objarr[mask])
KEYERROR:'[102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.10711.102711.102711.102711.102711. 102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.10711. 102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711.102711。 5337. 5337. 5337. 5337. 5337. 5337. 5337.\n 5337. 5337. 5337. 5337. 5337. 5337. 5337. 5337.\n 5337. 5337. 5337. 5337. 5337. 5337. 5337. 537.533 \n 5337. 5337. 5337. 5337. 5337. 5337. 5337. 5337.\n 5337. 5337. 5337. 5337. 5337. 5337. 5337. 5337.\n 5337. 5337. 21 24. 2124. 2124. 2124. 2124. 2124.\n 2124. 2124. 6643. 6643. 6643. 6643. 6643. 6643.\n 6643. 6643. 6643. 6643. 6643. 6643. \n64.663. 6643. 6643. 6643. 6643. 6643. 6643. 6643. 6643.\n 6643. 6643. 6643. 6643. 6643. 6643. 6643. 6643.] 不在索引中'
我是 python/pandas 的新手,非常感谢任何帮助,解决方案。
【问题讨论】:
-
所以您只需要将数值转换为
float而不是非数值? -
我需要确保它将 + 整数视为 TEXT/STRING,并且不会在最后添加实际显示在 excel 中的 .0(小数点)。
-
所以您需要将所有值转换为
typestring?问题是Excel解析int值转换为string为float? -
没错,AltID 中的任何值在 Pandas 中都应该被视为字符串
-
我试过你的解决方案 `first_pass_drop_duplicate.ix[first_pass_drop_duplicate.AltID.str.isdigit(), 'AltID'] = pd.to_numeric(first_pass_drop_duplicate.AltID, errors='coerce')
标签: python string pandas casting int