使用for binarizer和for loop替换列中每行的单元格值

2024-05-23 16:40:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一些帮助。我正在尝试更改.csv文件中的一列,其中有些是空的,有些是带有类别列表的。具体如下:

tdaa_matParent,tdaa_matParentQty
[],[]
[],[]
[],[]
[BCA_Aluminum],[1.3458]
[BCA_Aluminum],[1.3458]
[BCA_Aluminum],[1.3458]
[BCA_Aluminum],[1.3458]
[],[]
[Dye Penetrant Solution, BCA_Aluminum],[0.002118882, 1.3458]

但到目前为止,我只对第一列(tdaa\u matParent)进行了二值化,但无法将1替换为相应的数量值,如下所示。你知道吗

s = materials['tdaa_matParent']
mlb = MultiLabelBinarizer()
df = pd.DataFrame(mlb.fit_transform(s),columns=mlb.classes_)

BCA_Aluminum,Dye Penetrant Solution,tdaa_matParentQty
0,0,[]
0,0,[]
0,0,[]
1,0,[1.3458,0]
1,0,[1.3458,0]
1,0,[1.3458,0]
1,0,[1.3458,0]
0,0,[]
1,1,[1.3458,0.002118882]

但我真正想要的是每种色谱柱类别的一组新色谱柱(即BCA\ U铝和染料渗透液)。另外,如果填充,则每列都将被第二列的(tdaa\u matParentQty)值替换。你知道吗

例如:

BCA_Aluminum,Dye Penetrant Solution
0,0
0,0
0,0
1.3458,0
1.3458,0
1.3458,0
1.3458,0
0,0
1.3458,0.002118882

Tags: 文件csv列表数量类别solutiondyemlb
2条回答

对于问题中提供的示例数据,我将使用内置的Python方法来实现这一点:

from collections import OrderedDict
import pandas as pd

# simple case - material names are known before we process the data - allows to solve the problem with a single for loop
# OrderedDict is used to preserve the order of material names during the processing
base_result = OrderedDict([
    ('BCA_Aluminum', .0),
    ('Dye Penetrant Solution', .0)])
result = list()

with open('1.txt', mode='r', encoding='UTF-8') as file:

    # skip header
    file.readline()

    for line in file:

        # copy base_result to reuse it during the looping
        base_result_copy = base_result.copy()

        # modify base result only if there are values in the current line
        if line != '[],[]\n':
            names, values = line.strip('[]\n').split('],[')
            for name, value in zip(names.split(', '), values.split(', ')):
                base_result_copy[name] = float(value)

        # append new line (base or modified) to the result
        result.append(base_result_copy.values())

# turn list of lists into pandas dataframe
result = pd.DataFrame(result, columns=base_result.keys())
print(result)

输出:

   BCA_Aluminum  Dye Penetrant Solution
0        0.0000                0.000000
1        0.0000                0.000000
2        0.0000                0.000000
3        1.3458                0.000000
4        1.3458                0.000000
5        1.3458                0.000000
6        1.3458                0.000000
7        0.0000                0.000000
8        1.3458                0.002119

0.002119而不是0.002118882是因为默认情况下pandas显示float的方式,原始精度保留在dataframe中的实际数据中。你知道吗

谢谢!我构建了另一种同样有效的方法(尽管有点慢)。任何建议,请随时分享:)

df_matParent_with_Qty = pd.DataFrame()

# For each row in the dataframe (index and row´s column info),
for index, row in ass_materials.iterrows():

# For each row iteration save name of the element (matParent) and it´s index number:   
    for i, element in enumerate(row["tdaa_matParent"]):
#         print(i)
#         print(element)
# Fill in the empty dataframe with lists from each element
# And in each of their corresponding index (row), replace it with the value index inside the matParentqty list.
        df_matParent_with_Qty.loc[index,element] = row['tdaa_matParentQty'][i]

df_matParent_with_Qty.head(10)

相关问题 更多 >