在 pandas 中处理大数据集
Working with big dataset in pandas
我一直在使用这个数据集:https://www.kaggle.com/nsharan/h-1b-visa
我已将主数据框分成两部分:
soc_null 数据框 - 其中 SOC_NAME 列具有 NaN 值
soc_not_null - 其中 SOC_NAME 列的值不是 NaN
为了在 soc_null 数据框的 SOC_NAME 列中填充 NaN 值,我想出了这个代码:
for index1, row1 in soc_null.iterrows():
for index2, row2 in soc_not_null.iterrows():
if row1['JOB_TITLE'] == row2['JOB_TITLE']:
soc_null.set_value(index1,'SOC_NAME',row2['SOC_NAME'])
这个代码的问题是soc_null的长度是17734,soc_not_null的长度是2984724,我运行这个几个小时但是只有几个一百个值已更新,因此不可能在一台机器上完全执行这个 n^2 复杂性代码。
我相信必须有更好的方法来执行此操作,并且可能比我的数据集更大,因为清洁过程之后还有其他几个部分需要两个循环进行处理。
有些 可以解释您的需求。这是一种解决方案:
import pandas as pd
import numpy as np
values = [
{'JOB_TITLE':'secretary', 'SALARY':30000},
{'JOB_TITLE':'programmer', 'SALARY':60000},
{'JOB_TITLE':'manager', 'SALARY':None},
{'JOB_TITLE':'president', 'SALARY':None},
]
secret_values = [
{'JOB_TITLE':'manager', 'SALARY':150000},
{'JOB_TITLE':'president', 'SALARY':1000000},
]
df = pd.DataFrame(values)
df_secret = pd.DataFrame(secret_values)
df.set_index('JOB_TITLE', inplace=True)
df_secret.set_index('JOB_TITLE', inplace=True)
df.combine_first(df_secret).reset_index()
PS:避免在大型数据集上使用 for-each 循环。使用 Pandas.DataFrame 和其他优化的东西。
我一直在使用这个数据集:https://www.kaggle.com/nsharan/h-1b-visa
我已将主数据框分成两部分:
soc_null 数据框 - 其中 SOC_NAME 列具有 NaN 值
soc_not_null - 其中 SOC_NAME 列的值不是 NaN
为了在 soc_null 数据框的 SOC_NAME 列中填充 NaN 值,我想出了这个代码:
for index1, row1 in soc_null.iterrows():
for index2, row2 in soc_not_null.iterrows():
if row1['JOB_TITLE'] == row2['JOB_TITLE']:
soc_null.set_value(index1,'SOC_NAME',row2['SOC_NAME'])
这个代码的问题是soc_null的长度是17734,soc_not_null的长度是2984724,我运行这个几个小时但是只有几个一百个值已更新,因此不可能在一台机器上完全执行这个 n^2 复杂性代码。
我相信必须有更好的方法来执行此操作,并且可能比我的数据集更大,因为清洁过程之后还有其他几个部分需要两个循环进行处理。
有些
import pandas as pd
import numpy as np
values = [
{'JOB_TITLE':'secretary', 'SALARY':30000},
{'JOB_TITLE':'programmer', 'SALARY':60000},
{'JOB_TITLE':'manager', 'SALARY':None},
{'JOB_TITLE':'president', 'SALARY':None},
]
secret_values = [
{'JOB_TITLE':'manager', 'SALARY':150000},
{'JOB_TITLE':'president', 'SALARY':1000000},
]
df = pd.DataFrame(values)
df_secret = pd.DataFrame(secret_values)
df.set_index('JOB_TITLE', inplace=True)
df_secret.set_index('JOB_TITLE', inplace=True)
df.combine_first(df_secret).reset_index()
PS:避免在大型数据集上使用 for-each 循环。使用 Pandas.DataFrame 和其他优化的东西。