Select List python in a column included in all rows where a specific character

As shown below, find the list of [title] of this column, as long as there [China] these two specific characters, the line is located, all taken out.

 This table is defined as df:

for i in range(len(df)):
    a="中国"
    if a in df.loc[i,'title']:
        print(df.loc[i,:])

The results shown below, very unsightly, therefore, lists can be created, stored data.

                                                                                 

b1=[]
b2=[]
b3=[]

for i in range(len(df)):
    a="中国"
    if a in df.loc[i,'title']:
        a1=df.loc[i,'title']
        a2=df.loc[i,'content']
        a3=df.loc[i,'label']
        b1.append(a1)
        b2.append(a2)
        b3.append(a3)
        
f1=pd.DataFrame(columns=['title','content','label'])

f1['title']=b1
f1['content']=b2
f1['label']=b3

The results came out. As shown below:  

                                        

 

If you simply select an entire character in a column, that df [title] in this column, select the output contain the string [China] all the lines are located. As shown below:

df2=df[df['title'].isin(["中国输出"])]

 

 

Published 35 original articles · won praise 26 · views 80000 +

Guess you like

Origin blog.csdn.net/weixin_42342968/article/details/84879071