n1ce
n1ce

Reputation: 27

Remove item from list in pandas

how can I remove an item called 'heating' from a list which is used to do a specific value comparison in 2 different excel columns via pandas. As I need the item in another comparison I only want to exclude/remove it for that specific comparison.

from Attribute import unit
import pandas as pd
from pathlib import Path

# EXCEL DATA FRAME
fn = list(Path("C:\Compat\IN").glob("*.xlsx"))[0]
df_excel = pd.read_excel(fn, sheet_name="TriCad_Format")

# Attribute DATA FRAME with
# this dataframe is a list of lists and looks like that:
# units = [
#    ['reserve', 'TMYYY'],
#    ['heating', 'TM601'],
#    ['cooling', 'TM602']
#]
df_unit = pd.DataFrame(unit, columns=['Medium', 'machine'])

# Compare values
search_list_unit = df_unit['Medium'].unique().tolist()
match_units = df_excel[((df_excel["DBA03"].str.contains("|".join(search_list_unit), regex=True)) &
                         (~df_excel["DBA16"].isin(df_unit["machine"].tolist())))]

Can anyone help me out here? Thanks!

Upvotes: 0

Views: 337

Answers (1)

jfaccioni
jfaccioni

Reputation: 7529

Rather than removing the sublist containing 'heating' from your units list, it's easier to create a second dataframe by selecting rows whose Medium column is different from 'heating':

df_unit_without_heating = df_unit.loc[df_unit['Medium'] != 'heating']
print(df_unit_without_heating)

output:

    Medium machine
0  reserve   TMYYY
2  cooling   TM602

Upvotes: 1

Related Questions