简体   繁体   中英

Select columns from a dataframe based on values present

I have an excel file with many columns and rows. I want to select and import only some of these columns as long as they have right content (ie if any one of the column cells contains CUP )

I have the below code, but I its not working. my_excel照片

import pandas as pd
mystring = pd.read_excel("my_excel.xlsx", parse_cols='CUP' in col for cols in my_excel.xlsx, skiprows=[0])
mystring = data.to_string()
print(mystring)


SyntaxError: invalid syntax

Output should be like this:

mystring = 'SHOVEL2 CUP UMBRELLA WALLET MIKE GENARAL BASKET COFFEE TEA CUP SCREENING MOVIES'

I have attached my excel file photo here.

Like I mentioned, you'll need to import your data first , then filter it, and then join your filtered columns.

df = pd.read_excel("my_excel.xlsx", skiprows=[0])
m = df.isin(['CUP']).any(axis=0)
mystring = ' '.join(df.loc[:, m].values.ravel('F'))

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