要求:
数据帧中的一个特定列是“混合”类型。它可以有"123456""ABC12345"这样的值。
此数据框正在使用xlsxwriter写入Excel。
对于像"123456"这样的值,向下滚动熊猫将其转换为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

每次,我都会得到错误:
引发密钥错误('%s不在索引'%objarr[mask]中)
键错误:'[102711102711号102711号102711号。102711号。102711号。102711号102711。\n 102711。102711号。102711号。102711号。102711号。102711号。102711号。102711。\n 102711。102711号。102711号102711号102711号。102711号102711号。102711。\n 102711。102711号。102711号。102711号。102711号102711号。102711号。102711。\n 102711。102711号。102711号。102711号。102711号。102711号。102711号。102711。\n 102711102711号102711号。102711号102711号。102711号。102711号102711。\n 102711。102711号。102711号102711号。102711号。102711号。102711号。102711。\n 102711102711号102711号。102711号。102711号102711号102711号102711。\n 53375337号5337号。5337号5337号5337号5337号。5337.\n 5337。5337号。5337号5337号5337号。5337号。5337号。5337.\n 5337。5337号。5337号。5337号5337号5337号。5337号。5337.\n 5337。5337号。5337号5337号5337号。5337号。5337号5337.\n 5337。5337号。5337号。5337号。5337号。5337号。5337号。5337.\n 53375337号2124号。2124号。2124号2124号。2124号2124.\n 2124.2124号。6643号。6643号6643号6643号6643号。6643.\n 66436643号。6643号。6643号。6643号6643号。6643号。6643.\n 6643。6643号。6643号。6643号。6643号6643号。6643号。6643.\n 6643。6643号6643号。6643号6643号。6643号。6643号。6643.]不在索引中'
我是python/pandas的新手,任何帮助,解决方案都非常感谢。

最佳答案

我认为你需要:

df = pd.DataFrame({'AltID':['123456','ABC12345','123456'],
                   'B':[4,5,6]})

print (df)
      AltID  B
0    123456  4
1  ABC12345  5
2    123456  6

df.ix[df.AltID.str.isdigit(), 'AltID']  = pd.to_numeric(df.AltID, errors='coerce')

print (df)
      AltID  B
0    123456  4
1  ABC12345  5
2    123456  6

print (df['AltID'].apply(type))
0    <class 'float'>
1      <class 'str'>
2    <class 'float'>
Name: AltID, dtype: object

10-04 20:43