检查一个大文件.csv并替换,并将其分类在一列中
check a large file .csv and replace, and classify it in one column
我需要检查一个大文件.csv并替换,并在一栏中分类dataframe['value']
。
这是我的代码,但运行缓慢:
def encoder():
classes={}
a=dataframe['value']
c=-1
for i in a:
if i not in classes:
print(i)
c=c+1
classes[i]=c
for i in range(len(a)):
print(i)
dataframe['value'][i]=classes[a[i]]
有更好的解决方案吗?
这是我的数据集的一部分:
subject_id hadm_id seq_num icd9_code icustay_id value valueuom
18557 183341 4 42731 228376.0 Tracheostomy 0
18557 178725 4 42731 228376.0 Tracheostomy 0
18557 173656 5 42731 228376.0 Tracheostomy 0
18557 138883 10 42731 228376.0 Tracheostomy 0
18557 183341 4 42731 228376.0 30 0
18557 178725 4 42731 228376.0 30 0
18557 173656 5 42731 228376.0 30 0
18557 138883 10 42731 228376.0 30 0
18557 183341 4 42731 228376.0 2 Person Assist 0
18557 178725 4 42731 228376.0 2 Person Assist 0
18557 173656 5 42731 228376.0 2 Person Assist 0
18557 138883 10 42731 228376.0 2 Person Assist 0
18557 183341 4 42731 228376.0 Calm 0
18557 178725 4 42731 228376.0 Calm 0
18557 173656 5 42731 228376.0 Calm 0
18557 138883 10 42731 228376.0 Calm 0
18557 183341 4 42731 228376.0 Present 0
18557 178725 4 42731 228376.0 Present 0
18557 173656 5 42731 228376.0 Present 0
18557 138883 10 42731 228376.0 Present 0
我根据您的代码做出的一些假设,如果我错了请告诉我。
任务是对数据帧中 value 列的值进行编码。
我假设 value 列没有任何空值并且只有数字。
根据你的代码,我还假设你不允许使用像 sklearn 这样的库或任何东西来做这个。
在这些假设下,以下代码可能对您有所帮助。
x = sorted(dataframe["value"].unique())
y = list(range(0, len(x)))
encoded_dict = dict(zip(x, y))
dataframe["value"] = dataframe["value"].apply(lambda x: encoded_dict[x])
我需要检查一个大文件.csv并替换,并在一栏中分类dataframe['value']
。
这是我的代码,但运行缓慢:
def encoder():
classes={}
a=dataframe['value']
c=-1
for i in a:
if i not in classes:
print(i)
c=c+1
classes[i]=c
for i in range(len(a)):
print(i)
dataframe['value'][i]=classes[a[i]]
有更好的解决方案吗?
这是我的数据集的一部分:
subject_id hadm_id seq_num icd9_code icustay_id value valueuom
18557 183341 4 42731 228376.0 Tracheostomy 0
18557 178725 4 42731 228376.0 Tracheostomy 0
18557 173656 5 42731 228376.0 Tracheostomy 0
18557 138883 10 42731 228376.0 Tracheostomy 0
18557 183341 4 42731 228376.0 30 0
18557 178725 4 42731 228376.0 30 0
18557 173656 5 42731 228376.0 30 0
18557 138883 10 42731 228376.0 30 0
18557 183341 4 42731 228376.0 2 Person Assist 0
18557 178725 4 42731 228376.0 2 Person Assist 0
18557 173656 5 42731 228376.0 2 Person Assist 0
18557 138883 10 42731 228376.0 2 Person Assist 0
18557 183341 4 42731 228376.0 Calm 0
18557 178725 4 42731 228376.0 Calm 0
18557 173656 5 42731 228376.0 Calm 0
18557 138883 10 42731 228376.0 Calm 0
18557 183341 4 42731 228376.0 Present 0
18557 178725 4 42731 228376.0 Present 0
18557 173656 5 42731 228376.0 Present 0
18557 138883 10 42731 228376.0 Present 0
我根据您的代码做出的一些假设,如果我错了请告诉我。
任务是对数据帧中 value 列的值进行编码。
我假设 value 列没有任何空值并且只有数字。
根据你的代码,我还假设你不允许使用像 sklearn 这样的库或任何东西来做这个。
在这些假设下,以下代码可能对您有所帮助。
x = sorted(dataframe["value"].unique())
y = list(range(0, len(x)))
encoded_dict = dict(zip(x, y))
dataframe["value"] = dataframe["value"].apply(lambda x: encoded_dict[x])