[Pandas] Creating multiple with dataframe for statements.

Asked 1 years ago, Updated 1 years ago, 344 views

Hello, I'm a beginner who just started playing pandas. I'm a python. It's just that I'm making a df in Dataframe that contains certain words, and that particular word gets some value from the array.

array

But I need to make double for statements that fit the arrangement of df one by one, but is there a way to make multiple for statements or other df with the code I made in the example below?

Please give us good suggestions.

trp1 = df[(df['ITEM'].str.contains(pat[0])) & (df['ITEM'].str.contains(mux[0]))].astype(float)

trp2 = df[(df['ITEM'].str.contains(pat[0])) & (df['ITEM'].str.contains(mux[1]))].astype(float)

trp3 = df[(df['ITEM'].str.contains(pat[0])) & (df['ITEM'].str.contains(mux[2]))].astype(float)

trp4 = df[(df['ITEM'].str.contains(pat[0])) & (df['ITEM'].str.contains(mux[3]))].astype(float)

trp5 = df[(df['ITEM'].str.contains(pat[0])) & (df['ITEM'].str.contains(mux[4]))].astype(float)

pandas dataframe

2023-03-24 13:33

1 Answers

trps=[]
for _ in [1,2,3,4,5]:
    tmp = df[(df['ITEM'].str.contains(pat[0])) & (df['ITEM'].str.contains(mux[_]))].astype(float)
    trps.append(tmp)


2023-03-25 09:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.