Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Editing row after finding all its values with Pandas

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading