牛蒡是什么| 脂肪肝看什么科| 法国用什么货币| 芨芨草长什么样图片| 情绪价值是什么意思| 癣是什么原因引起的| 为什么会被限制高消费| 8.23是什么星座| 大安是什么意思| 天成是什么意思| 泡沫尿吃什么药| 头皮屑挂什么科| 腺样体肥大是什么症状| 宫腔镜是什么手术| b-h是什么药| 癫痫病是什么原因引起的| 宣府是现在的什么地方| 经期喝茶有什么影响| 内服什么可以美白全身| 放疗后吃什么恢复快| 做梦梦到屎什么意思| 康乃馨适合送什么人| 叔叔的儿子叫什么| 什么身什么骨| 九寨沟在什么地方| 备皮什么意思| 2017年是什么命| 躺下就头晕是什么原因| 连什么带什么| 清明节的习俗有什么| 平起平坐是什么动物| 小孩说梦话是什么原因引起的| 光明磊落是什么生肖| 喝最烈的酒下一句是什么| 什么是复句| 眩晕症是什么原因造成的| 玫琳凯属于什么档次| 什么是修行人| 备孕检查都检查什么项目| 商务专员是做什么的| 为什么会打哈欠| 越什么越什么| 长期便秘喝什么茶好| 财主代表什么生肖| 女性分泌物发黄是什么原因| 耳朵闷闷的堵住的感觉是什么原因| 温居是什么意思| 荣字五行属什么| 胃窦黄斑瘤是什么病| 老是咳嗽挂什么科| 羊水多了对宝宝有什么影响| 至字五行属什么| 什么叫子宫肌瘤| 一片狼藉是什么意思| 儿童腮腺炎吃什么药| 大人是什么意思| 近视眼镜是什么镜| 备孕检查什么项目| 7月出生的是什么星座| 中国第一艘航空母舰叫什么| 高铁动力来源是什么| 为什么会有阴道炎| 下腹痛挂什么科| 日语八嘎是什么意思| 什么是美尼尔氏综合症| 猫咪踩奶是什么意思| 什么样的人容易得结石| cno什么意思| 种马什么意思| 叒字什么意思| 一进去就射是什么原因| 腿肿挂什么科| 米黄是什么颜色| 营长是什么级别| 为什么叫老鸨| 什么颜色混合是红色| 舌头干燥是什么原因| 吆西是什么意思| 肩胛骨疼是什么原因| 胃痛吃什么药效果好| 沐五行属性是什么| 为什么牙疼| 维和部队是干什么的| 病入膏肓什么意思| 直肠给药对小孩身体有什么影响| 早晨起来口苦什么原因| 桂字五行属什么| 达英35是什么| 人次什么意思| 防是什么生肖| 咽喉炎是什么症状| 结婚30年是什么婚姻| 白兰地属于什么酒| 向日葵代表什么| 口腔溃疡喝什么水| 5月23日是什么日子| 宝宝经常发烧是什么原因引起的| 肾不纳气用什么中成药| 蒸馏水是什么| 1939年中国发生了什么| 拉肚子喝什么| 卡哇伊内是什么意思| 榴莲是什么季节的| 乳房疼痛吃什么消炎药| 月经突然提前一周是什么原因| 无力感什么意思| 体重突然下降是什么原因| 空你几哇什么意思| 吃了兔子肉不能吃什么| 吃什么能让胸变大| 218号是什么星座| amy什么意思| 等闲识得东风面什么意思| hp是什么单位| 过敏什么东西不能吃| p.a.是什么意思| 喝苦荞茶有什么好处和坏处| 吃什么排铅效果最好| 屁股痛是什么原因| 集体户口是什么意思| 不遗余力什么意思| 属蛇的是什么星座| 莱赛尔是什么面料| 气短吃什么药立马见效| 虾仁可以炒什么菜| 补钙过量有什么害处| 射频消融术是什么手术| 人为什么会打喷嚏| 货号是什么| 离婚需要带什么证件| 什么止疼药见效最快| 前列腺特异性抗原是什么意思| 满载而归的载是什么意思| 国树是什么树| 1992年什么命| 吃花生米有什么好处| 童养媳是什么意思| ed是什么病| 抑郁什么意思| 亚甲炎是什么原因引起的| 食管裂孔疝是什么病| 牛仔裤配什么上衣| 特别想睡觉是什么原因| 厚颜无耻是什么生肖| 当今社会什么行业前途比较好| 叶酸片什么时候吃合适| EXP什么意思| 端午节应该吃什么| 三严三实是什么| 脑电图异常是什么病| 3月23日什么星座| 秋天穿什么衣服| 鼻咽炎吃什么药| 执业药师什么时候报名| 胃粘膜糜烂吃什么药| 5月11日什么星座| 烟嗓是什么意思| 茶不能和什么一起吃| 高潮是什么感觉| 宫颈光滑说明什么| 梦见自己大出血是什么征兆| 鸡蛋和面粉可以做什么好吃的| 脊柱侧弯挂什么科| 刺猬的刺有什么作用| hpv42阳性是什么意思| 囊肿吃什么药| 姓氏是什么意思| 尿道炎和阴道炎有什么区别| 广州白云区有什么好玩的地方| 得了便宜还卖乖是什么意思| 气体交换受损与什么有关| 蜥蜴人是什么| 晚上睡不着是什么原因| 高丽参和红参有什么区别| 1月19号什么星座| 回族为什么姓马的多| 你是什么意思| 什么样的伤口算开放性| 跑步机cal是什么意思| 胸口痛吃什么药| 奶油是什么做的| 拉黑粑粑是什么原因啊| SEX是什么| 什么时候放暑假| 清浅是什么意思| s和m是什么意思啊| 好强的女人是什么性格| 高密度脂蛋白胆固醇高是什么意思| 碧玺五行属什么| 萎缩性胃炎不能吃什么食物| 阳痿吃什么好| 肺部疼痛什么原因| 信佛有什么好处| 什么是眩晕症| 腰痛是什么原因引起的| 木耳不能和什么一起吃| 夹层是什么意思| 接济是什么意思| 大方得体是什么意思| 吃什么油对心脑血管好| FAN英语什么意思| 罗汉果有什么功效| 处暑是什么时候| 钙化积分是什么意思| jz是什么意思| 氧分压高是什么原因| 投诉护士找什么部门| 人是什么结构| 舅舅的女儿叫什么| 晚上看见蛇有什么预兆| 早上左眼跳是什么预兆| darling是什么意思| 吃优甲乐不能吃什么| 蛋糕用什么面粉| 青光眼用什么眼药水| 冰箱什么品牌最好| 英短蓝猫吃什么猫粮好| 猫三联什么时候打| vr间隙是什么意思| 太子是什么生肖| 皮肤瘙痒用什么药治疗| 7月20日什么星座| 什么样的眼睛形容词| 巴宝莉是什么品牌| 精工手表什么档次| 木石念什么| 眼干眼涩用什么眼药水| 经常失眠是什么原因| 肝硬化是什么意思| 上火牙齿痛吃什么药| 福兮祸兮是什么意思| 查甲状腺应该挂什么科| 五行缺什么| 海棠是什么| 特别提款权是什么意思| 断眉有什么说法| 腹部彩超挂什么科| 头疼流鼻血是什么原因| 花千骨什么时候上映的| 乳腺钙化是什么意思| 受罪是什么意思| 胳膊脱臼什么症状| 核磁共振什么时候出结果| 老虎最怕什么| 什么头什么发| 急性鼻窦炎吃什么药| 贫血的人吃什么水果| 激光脱毛对人体有没有什么危害| 阴吹是什么| 淋巴细胞计数偏低是什么原因| 上海话册那是什么意思| 什么是什么的摇篮| 喜欢白色的人是什么性格| 脑癌是什么原因引起的| 声音嘶哑吃什么药好| 哀怨是什么意思| 有冬瓜西瓜南瓜为什么没有北瓜| 全腹部ct平扫主要检查什么| 世界上笔画最多的字是什么字| 心动过缓吃什么药| 海马萎缩是什么情况| 9月10日什么星座| 威士忌兑什么饮料好喝| 草缸适合养什么鱼| 百度
 

sjh是什么意思

百度 第二最重要是节奏控制,我们下一步需要在这方面进行提高。

A Data Scientist’s perspective on SQL-like Python functions.





Photo by?Bruce Hong?on?Unsplash?[1].

 

Table of Contents

 
 

  1. Introduction
  2. Multiple Conditions
  3. Merging On Multiple, Specific Columns
  4. Summary
  5. References

 

Introduction

 
 
Whether you are transitioning from a data engineer/data analyst or wanting to become a more efficient?data scientist, querying your dataframe can prove to be quite a useful method of returning specific rows that you want. It is important to note that there is a specific query function for pandas, appropriately named,?query. However, I will instead be discussing the other ways that you can mimic querying, filtering, and merging your data. We will present common scenarios or questions that you would ask to your data, and rather than SQL, we will do it with Python. In the paragraphs below, I will outline some simple ways of querying rows for your pandas dataframe with the Python programming language.

 

Multiple Conditions

 
 



Sample data. Screenshot from Author [2].

 

As data scientists or data analysts, we want to return specific rows of data. One of these scenarios is where you want to apply multiple conditions, all in the same line of code. In order to display my example, I have created some fake sample data of a first and last name, as well as their respective gender and birthdate. This data is displayed above in the screenshot.

The example multiple conditions will essentially answer a specific question, just like when you use SQL. The question is, what percent of our data is Male gender?OR?a person who was born between 2010 and 2021.

Here is the code that will solve that question (there are a few ways to answer this question, but here is my specific way of doing it):

print(“Percent of data who are Males OR were born between 2010 and 2021:”,
 100*round(df[(df[‘Gender’] == ‘M’) | (df[‘Birthdate’] >= ‘2010–01–01’) & 
 (df[‘Birthdate’] <= ‘2021–01–01’)][‘Gender’].count()/df.shape
 [0],4), “%”)

 

To better visualize this code, I have also included this screenshot of that same code from above, along with the output/result. You can also apply these conditions to return the actual rows instead of getting the fraction or percent of rows out of the total rows.



Conditions code. Screenshot by Author [3].

 

Here is the order of commands we performed:

  • Return rows with Male?Gender
  • Include the?OR?function?|
  • Return the rows of?Birthdate?> 2010 and 2021
  • Combine those all, and then divide by the total amount of rows

As you can see, this code is similar to something you would see in SQL. I personally think it is easier in pandas because it can be less code, while also being able to visually see all the code in one easy spot, without having to scroll up and down (but this format is just my preference).

 

Merging On Multiple, Specific Columns

 
 



Merged dataframe result. Screenshot by Author [4].

 

We have probably seen how to merge dataframes together in other tutorials, so I wanted to add a unique approach that I have not really seen out there, which is merging on multiple, specific columns. In this scenario, we want to join two dataframes where two fields are shared between them. You could tell that if there are even more columns, this method could be even more useful.

We have our first dataframe, which is df, then we are merging our columns on a second dataframe, df2. Here is that code to achieve our expected result:

merged_df = df.merge(df2, how=’inner’, 
 left_on=cols, 
 right_on=cols
 )

 

To better visualize this merging and code, I have presented the screenshot below. You see what the second dataframe looks like below, with the?First?and?Last?names, just like they are in the first dataframe, but with a new column,?Numeric. Then, we have out specific columns that we wanted to merge on, while returning columns?Gender,?Birthdate, and the new?Numeric?column as well. The columns are a list of columns, which is named?cols.



Merging dataframe. Screenshot by Author [5].

 

As you can see, this way of merging dataframes is a simple way to achieve the same results that you would get from a SQL query.

 

Summary

 
 
In this tutorial, we saw two common questions or queries that you would perform in SQL, but instead, have performed them with pandas dataframes in Python.

To summarize, here are the two scenarios we worked with:

  1. Returning the percent of rows out of the total dataset from multiple conditions
  2. Merging on multiple, specific columns to return a final dataframe with a new column

 

I hope you found my article both interesting and useful. Please feel free to comment down below if you agree or disagree with these methods. Why or why not? These can certainly be clarified even further, but I hope I was able to shed some light on some of the ways you can use pandas and Python instead of SQL. Thank you for reading!

Please feel free to check out my profile, Matt Przybyla, and other articles, as well as reach out to me on LinkedIn.

I am not affiliated with these companies.

 
References

[1] Photo by?Bruce Hong?on?Unsplash, (2018)

[2] M. Przybyla, sample data screenshot, (2021)

[3] M. Przybyla, conditions code screenshot, (2021)

[4] M. Przybyla, merged dataframe result screenshot, (2021)

[5] M. Przybyla, merging dataframe screenshot, (2021)

 
Bio: Matthew Przybyla is Senior Data Scientist at Favor Delivery, and a freelance technical writer, especially in data science.

Original. Reposted with permission.

Related:



e-mail什么意思 成熟是什么意思 狐狸和乌鸦告诉我们什么道理 鸽子夏天喝什么水好 细思极恐是什么意思
鼻子老是出血是什么原因 为什么喉咙痛 血小板是什么意思 日不落是什么意思 脸上涂什么可以美白
这个季节适合种什么菜 娇羞是什么意思 光是什么意思 苦丁茶有什么作用和功效 告示是什么意思
月柱华盖是什么意思 大快朵颐是什么意思 浅表性胃炎吃什么药好 肩胛骨疼挂什么科 省纪委常委是什么级别
属羊的守护神是什么菩萨hcv8jop9ns8r.cn 围度什么意思gysmod.com 山东特产是什么hcv8jop5ns7r.cn 无花果吃多了有什么坏处hcv9jop3ns9r.cn 左脸颊有痣代表什么jasonfriends.com
尿道感染有什么症状hcv8jop5ns9r.cn 叶酸是什么东西hcv7jop5ns6r.cn 哆啦a梦的寓意是什么hcv7jop4ns7r.cn 凉面是用什么面做的hcv8jop8ns6r.cn 一月10号是什么星座1949doufunao.com
fvc是什么意思hcv9jop0ns9r.cn 排骨炖什么好吃hcv7jop6ns8r.cn 穿山甲说了什么hcv8jop6ns0r.cn 什么东西嘴里没有舌头hcv8jop4ns9r.cn 2005年属鸡是什么命hcv9jop4ns0r.cn
38年属什么生肖hcv8jop3ns9r.cn 男生学什么技术吃香hkuteam.com 小孩腿抽筋是什么原因引起的hcv8jop6ns7r.cn 羊肉汤放什么调料hcv8jop4ns0r.cn 四相是什么意思hcv8jop8ns1r.cn
百度