需求:
数据框中的某一列是“混合”类型。它可以具有类似于 "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不在索引中' % objarr[mask])
KeyError: '[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. 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. 102711. 102711. 102711. 102711. 102711. 102711.
5337. 5337. 5337. 5337. 5337. 5337. 5337. 5337.
5337. 5337. 5337. 5337. 5337. 5337. 5337. 5337.
5337. 5337. 5337. 5337. 5337. 5337. 5337. 5337.
5337. 5337. 5337. 5337. 5337. 5337. 5337. 5337.
5337. 5337. 5337. 5337. 5337. 5337. 5337. 5337.
5337. 5337. 2124. 2124. 2124. 2124. 2124. 2124.
2124. 2124. 6643. 6643. 6643. 6643. 6643. 6643.
6643. 6643. 6643. 6643. 6643. 6643. 6643. 6643.
6643. 6643. 6643. 6643. 6643. 6643. 6643. 6643.]不在索引中'
我是Python / Pandas的新手,非常感谢任何帮助和解决方案。
float
,而非数字则不需要转换是吗? - jezraelstring
类型:first_pass_drop_duplicate.ix[first_pass_drop_duplicate.AltID.astype(str).str.isdigit(), 'AltID'] = pd.to_numeric(first_pass_drop_duplicate.AltID, errors='coerce')
- jezrael