简体   繁体   中英

How to use a pd.concat in a for loop

x = "'1','2','3','4'"
for i in x.split(","):
      a = (df[df['Column'].str.contains("'"+i+"'")])
      ## I want to write all a values side by side like this.
      ## pd.concat(a,a,a,a,a,a,a,......)

Hello everyone, As you can see. I have a string and I will filter certain columns from the dataframe with the values in this string

df.str.contains takes a maximum of 6 values, Thus I want to filter separately and collect dataframes at the end.

Therefore i used pd.concat, but i dont remember and couldnt find any way to find typing all the a values.

I need your help. Thanks in advance, Best.

you can actually use a list and concat later on:

x = "'1','2','3','4'"
results = []
for i in x.split(","):
      a = (df[df['Column'].str.contains("'"+i+"'")])
      results.append(a)

pd.concat(results)

This should be faster:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM