Home > Software engineering >  Editing row after finding all its values with Pandas
Editing row after finding all its values with Pandas

Time:05-05

Trying to edit a cell after finding row through user input.

df = pd.read_excel(file)

memory = input("Select Memory : ")

# Match and keep columns
a = df.columns
b = ['IDC S/N', 'ECC', 'Cabinet Qty', 'MMEX', 'VDR']
keep_columns = [x for x in a if x in b]

# Look within IDC S/N for user input 
df = df.loc[df.loc[:, 'IDC S/N'].fillna('nan').str.lower().str.contains(memory.lower()), :, ][
                    keep_columns]
                df.reset_index(drop=True, inplace=True)
print(df)

Output :

   ECC    IDC S/N  Cabinet Qty  MMEX  VDR
0  Yes  H532G070S           19     8    0

I would like to edit the Cabinet Qty.

What was tried so far :

df.loc[df.loc[:, "IDC S/N"].fillna('nan').str.lower().str.contains(memory.lower()), :, ]['Cabinet Qty']

Output:

# 19 Equals Cabinet Qty 
8    19
Name: Cabinet Qty, dtype: int64

Yet this one throws an error :

S/N"].fillna('nan').str.lower().str.contains(memory.lower()), :, ]['Cabinet Qty'] == ['50']

Output:

    raise ValueError(
ValueError: ('Lengths must match to compare', (0,), (1,))

Help will be highly appreciated!

CodePudding user response:

Your solution is possible simplify - removed fillna, because possible add na parameter to Series.str.contains and removed double [] for use DataFrame.loc:

df.loc[df["IDC S/N"].str.lower().str.contains(memory.lower(), na=False), 'Cabinet Qty'] = 50
  • Related