dataframe设置两个条件取值的实例
如下所示:
>>>importpandasaspd
>>>importnumpyasnp
>>>frompandasimportSeries,DataFrame
>>>df=DataFrame({'name':['a','a','b','b'],'classes':[1,2,3,4],'price':[11,22,33,44]})
>>>df
classesnameprice
01a11
12a22
23b33
34b44
>>>
根据index和columns取值
>>>s=df.loc[0,'price'] >>>s 11
根据同行的columns的值取同行的另一个columns的值
>>>sex=df.loc[(df.classes==1)&(df.name=='a'),'price'] >>>sex 011 Name:price,dtype:int64 >>>sex=df.loc[(df.classes==1)&(df.name=='a'),'price'].values[0] >>>sex 11
根据条件同时取得多个值
>>>name,price=df.loc[df.classes==1,('name','price')].values[0]
>>>name
'a'
>>>price
11
>>>
对一列赋值
>>>df.loc[:,'price']=0 >>>df classesnameprice 01a0 12a0 23b0 34b0 >>>
对df的一个列进行函数运算
【1】 >>>df['name']=df['name'].apply(lambdax:x.upper()) >>>df classesnameprice 01A11 12A22 23B33 34B44 【2】 >>>df.loc[:,'name']=df['name'].apply(lambdax:x.upper()) >>>df classesnameprice 01A11 12A22 23B33 34B44 >>>
对df的几个列进行函数运算
【1】 >>>df[['classes','price']]=df[['classes','price']].applymap(lambdax:str(x)) >>>print(type(df.loc[0,"classes"]))>>>print(df.loc[0,"classes"]) 1 【2】 >>>df.loc[:,['classes','price']]=df[['classes','price']].applymap(lambdax:int(x)) >>>print(type(df.loc[0,"classes"])) >>>print(df.loc[0,"classes"]) 1 >>>
对两个列进行去重
>>>df classesnameprice 01a11 11a22 23b33 34b44 >>>df.drop_duplicates(subset=['classes','name'],inplace=True) >>>df classesnameprice 01a11 23b33 34b44
多个条件分割字符串
>>>fund_memeber='赵四、王五'
>>>fund_manager_list=re.split('[;,、]',fund_memeber)
>>>fund_manager_list
['赵四','','王五']
#DataFrame构造器
>>>df=DataFrame({'x':[1],'y':[2]})
>>>df
xy
012
>>>
删除某列值为特定值得那一行
>>>df=DataFrame({'name':['a','b','c','d'],'classes':[1,2,3,4],'price':[11,22,33,44]})
>>>df
classesnameprice
01a11
12b22
23c33
34d44
【方法一】
>>>df=df.loc[df['name']!='a']
>>>df
classesnameprice
12b22
23c33
34d44
>>>
【方法二】
df.drop(df[df.name=='a'].index,axis=0)
#筛选df的每列值包含某个字段‘/a'
>>>importpandasaspd
>>>df=pd.DataFrame({'a':['A','B'],'b':['AA','BB']})
>>>df
ab
0AAA
1BBB
>>>df[df['a'].str.contains(r'A')]
ab
0AAA
>>>df=pd.DataFrame({'a':['/api/','B'],'b':['AA','BB']})
>>>df
ab
0/api/AA
1BBB
>>>df[df['a'].str.contains(r'/api/')]
ab
0/api/AA
>>>
把列变成index和把index变成列
df
request_urlvisit_times
9fofeasy_产品基本信息7
8投顾挖掘6
5投顾挖掘5
6投顾挖掘5
7fofeasy_产品基本信息5
3fofeasy_产品基本信息4
4fofeasy_产品基本信息4
2投顾挖掘2
0行业数据——其他1
1行业数据——其他1
x=df.set_index('request_url')
x
visit_times
request_url
fofeasy_产品基本信息7
投顾挖掘6
投顾挖掘5
投顾挖掘5
fofeasy_产品基本信息5
fofeasy_产品基本信息4
fofeasy_产品基本信息4
投顾挖掘2
行业数据——其他1
行业数据——其他1
x.reset_index('request_url')
request_urlvisit_times
0fofeasy_产品基本信息7
1投顾挖掘6
2投顾挖掘5
3投顾挖掘5
4fofeasy_产品基本信息5
5fofeasy_产品基本信息4
6fofeasy_产品基本信息4
7投顾挖掘2
8行业数据——其他1
9行业数据——其他1
pandas按照列A分组,将同一组的列B求和,生成新的Dataframe
>>>df.groupby(by=['request_url'])['visit_times'].sum() >>> request_url fofeasy_产品基本信息20 投顾挖掘18 行业数据——其他2 Name:visit_times,dtype:int64
dict变成dataframe
In[15]:dict=pd.DataFrame({'x':1,'y':2},index=[0])
In[16]:dict
Out[16]:
xy
012
iloc
In[69]:df1.iloc[1:5,2:4] Out[69]: 46 20.301624-2.179861 41.462696-1.743161 61.3142320.690579 80.0148713.357427
以上这篇dataframe设置两个条件取值的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。