Python,如何将一个数据帧划分为两个新的数据帧

时间:2019-09-24 21:55:27

标签: python pandas dataframe filter

我想将这些数据帧分成两个新的数据帧。

1)包含所有Google

2)包含所有bing

通过匹配字符串是有效的..但是通过查找以“ Bing”或“ Google”开头的任何内容都会更好!


Dataframe:

   number   platform         rds

0  200      BingBrand         56
1  200      BingNonBrand      56
2  200      BingNonBrand      56
3  151      GoogleNonBrand    56
4  1651     GoogleDisplay     56     
5  626      Bing              56
6  626      BingNonBrand      56
7  125      GoogleBrand       56



# It is working but only for the last condition in the brackets, 
example: BingBrand, GoogleBrand


# creating two new dataframes for each

cw_bing = cw[cw["platform"] == ('Bing' and 'BingNonBrand' and 'BingBrand' )]

cw_google = cw[cw["platform"] == ('GoogleNonBrand'and 'GoogleDisplay' 
and 'Google' and 'GoogleBrand')]



尝试按以“必应”或“ Google”开头的任何行进行拆分


# Doesnt work

# cw_bing = cw[cw["vendorname"].filter(regex='Bing.*')]
# cw_google = cw[cw["vendorname"].filter(regex='Google.*')]


最终结果应该是这样

df 1 = 

            BingBrand         56
1  200      BingNonBrand      56
2  200      BingNonBrand      56
5  626      Bing              56
6  626      BingNonBrand      56

df2 = 

3  151      GoogleNonBrand    56
4  1651     GoogleDisplay     56     
7  125      GoogleBrand       56

1 个答案:

答案 0 :(得分:1)

您可以使用contain。

bing = df[df.platform.str.contains('Bing')]

google = df[df.platform.str.contains('Google')]