尝试使用 Pandas 为某些列分配更改的值时出现问题
Issue while trying assigning changed values for certain columns with Pandas
所以我抓取了数据并按我喜欢的方式保存了它,现在我正在清理中。但是尽管我使用 pandas 创建了新文件,但在保存更改时遇到问题。
我缺少什么,为什么它不分配我 iter 到原始值?
我找到了一些关于 apply() 的信息,但它对我没有用。相反,我曾经得到空行。
一种
我的代码:
import pandas as pd
needtoclean = pd.read_excel(r'C:\Users\sound\Desktop\data.xlsx')
#making all into str, due its mixed with int,float,str
needtoclean['Salary'] = needtoclean['Salary'].astype(str)
columnSeriesObj = needtoclean['Salary']
columnSeriesObj = needtoclean['Salary']
for value in columnSeriesObj.values:
#print(value)
value = value.replace('Nuo',"").replace('Iki','')
value = value.strip()
value = re.split(r'[\s,-]+', value)
if len(value) > 1: #
value = (int(value[0])+int(value[1]))/2
else:
value = float(value[0])
value = round(value)
#print(value)
columnTitle = needtoclean['Job Title']
for value in columnTitle.values:
#print(value)
value = value.title()
value = value.replace('/', ' ').replace('(-Ė)','').replace('(-A)','').replace('(-As)', '')
value = value.strip()
value = value.split()
#print(value)
needtoclean.to_excel("datatest.xlsx")
工资原值:
Nuo 2600
2000-2500
1487-2479
Iki 3636
1600-5700
1200-2000
我排除了:
2600
2250
1983
3636
3650
1600
职位:
IT infrastruktūros palaikymo skyriaus vadovas (-ė)
Bankinių kortelių skaitytuvų programuotojas (-a)
IT sistemų priežiūros skyriaus vadovas (-ė)
Produktų palaikymo skyriaus vadovas (ė) (programinė įranga)
预计获得:
['It', 'Infrastruktūros', 'Palaikymo', 'Skyriaus', 'Vadovas']
['Bankinių', 'Kortelių', 'Skaitytuvų', 'Programuotojas']
['It', 'Sistemų', 'Priežiūros', 'Skyriaus', 'Vadovas']
['Produktų', 'Palaikymo', 'Skyriaus', 'Vadovas', '(Ė)', '(Programinė', 'Įranga)']
只需创建空列表,在其中附加值,用新值替换旧列,其他列也类似。
salary_cleaned_values = []
columnSeriesObj = needtoclean['Salary']
for value in columnSeriesObj.values:
#print(value)
value = value.replace('Nuo',"").replace('Iki','')
value = value.strip()
value = re.split(r'[\s,-]+', value)
if len(value) > 1: #
value = (int(value[0])+int(value[1]))/2
else:
value = float(value[0])
value = round(value)
salary_cleaned_values.append(value)
needtoclean['Salary'] = salary_cleaned_values
所以我抓取了数据并按我喜欢的方式保存了它,现在我正在清理中。但是尽管我使用 pandas 创建了新文件,但在保存更改时遇到问题。 我缺少什么,为什么它不分配我 iter 到原始值? 我找到了一些关于 apply() 的信息,但它对我没有用。相反,我曾经得到空行。 一种 我的代码:
import pandas as pd
needtoclean = pd.read_excel(r'C:\Users\sound\Desktop\data.xlsx')
#making all into str, due its mixed with int,float,str
needtoclean['Salary'] = needtoclean['Salary'].astype(str)
columnSeriesObj = needtoclean['Salary']
columnSeriesObj = needtoclean['Salary']
for value in columnSeriesObj.values:
#print(value)
value = value.replace('Nuo',"").replace('Iki','')
value = value.strip()
value = re.split(r'[\s,-]+', value)
if len(value) > 1: #
value = (int(value[0])+int(value[1]))/2
else:
value = float(value[0])
value = round(value)
#print(value)
columnTitle = needtoclean['Job Title']
for value in columnTitle.values:
#print(value)
value = value.title()
value = value.replace('/', ' ').replace('(-Ė)','').replace('(-A)','').replace('(-As)', '')
value = value.strip()
value = value.split()
#print(value)
needtoclean.to_excel("datatest.xlsx")
工资原值:
Nuo 2600
2000-2500
1487-2479
Iki 3636
1600-5700
1200-2000
我排除了:
2600
2250
1983
3636
3650
1600
职位:
IT infrastruktūros palaikymo skyriaus vadovas (-ė)
Bankinių kortelių skaitytuvų programuotojas (-a)
IT sistemų priežiūros skyriaus vadovas (-ė)
Produktų palaikymo skyriaus vadovas (ė) (programinė įranga)
预计获得:
['It', 'Infrastruktūros', 'Palaikymo', 'Skyriaus', 'Vadovas']
['Bankinių', 'Kortelių', 'Skaitytuvų', 'Programuotojas']
['It', 'Sistemų', 'Priežiūros', 'Skyriaus', 'Vadovas']
['Produktų', 'Palaikymo', 'Skyriaus', 'Vadovas', '(Ė)', '(Programinė', 'Įranga)']
只需创建空列表,在其中附加值,用新值替换旧列,其他列也类似。
salary_cleaned_values = []
columnSeriesObj = needtoclean['Salary']
for value in columnSeriesObj.values:
#print(value)
value = value.replace('Nuo',"").replace('Iki','')
value = value.strip()
value = re.split(r'[\s,-]+', value)
if len(value) > 1: #
value = (int(value[0])+int(value[1]))/2
else:
value = float(value[0])
value = round(value)
salary_cleaned_values.append(value)
needtoclean['Salary'] = salary_cleaned_values