Python Pandas Tutorial (Part 6): Add/Remove Rows and Columns From DataFrames

Поделиться
HTML-код
  • Опубликовано: 1 авг 2024
  • In this video, we will be learning how to add and remove our rows and columns.
    This video is sponsored by Brilliant. Go to brilliant.org/cms to sign up for free. Be one of the first 200 people to sign up with this link and get 20% off your premium subscription.
    In this Python Programming video, we will be learning how to add and remove rows and columns from dataframes using the append and drop methods. We will also see how we can create new columns by combining elements from existing ones. Let's get started...
    The code for this video can be found at:
    bit.ly/Pandas-06
    StackOverflow Survey Download Page - bit.ly/SO-Survey-Download
    ✅ Support My Channel Through Patreon:
    / coreyms
    ✅ Become a Channel Member:
    / @coreyms
    ✅ One-Time Contribution Through PayPal:
    goo.gl/649HFY
    ✅ Cryptocurrency Donations:
    Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
    Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
    Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
    ✅ Corey's Public Amazon Wishlist
    a.co/inIyro1
    ✅ Equipment I Use and Books I Recommend:
    www.amazon.com/shop/coreyschafer
    ▶️ You Can Find Me On:
    My Website - coreyms.com/
    My Second Channel - / coreymschafer
    Facebook - / coreymschafer
    Twitter - / coreymschafer
    Instagram - / coreymschafer
    #Python #Pandas

Комментарии • 240

  • @coreyms
    @coreyms  4 года назад +72

    Hey everyone. Hope you're all having a great weekend. I'm still working on finishing up the series. Let me know if there is anything specific you want me to cover. In the next video I'll be covering sorting, and some videos still to come include: working with dates, aggregating data, reading and writing to/from different sources, and a discussion on the recent Pandas version 1.0 release. Let me know if there is anything else you'd like to see! Thanks and have a good one!

    • @shamsuddinjunaid30
      @shamsuddinjunaid30 4 года назад +7

      Hey Corey!! Could you please dedicate one of the videos to visualisations using Pandas...Thanks:)

    • @y0ngjin
      @y0ngjin 4 года назад

      Hi Corey, Can you please cover lambda? Thank you as always!

    • @coreyms
      @coreyms  4 года назад +17

      Shamsuddin Junaid Yes. I’m actually going to do an entire series just for Pandas plotting

    • @kaaaaasraa49563
      @kaaaaasraa49563 4 года назад +1

      Hi Corey,
      Please make a video about how to sum columns or rows in our DataFrame(each column/row or with a specific condition)
      I think it will be really helpful in some cases...
      Thanks🙏🙏🙏

    • @chrizantik
      @chrizantik 4 года назад +1

      A video on Dask would be great. Keep up the great work!

  • @andynelson2340
    @andynelson2340 2 года назад +51

    8:00 if you're getting a FutureWarning on append method, you can you concat method instead. df = pd.concat([df, pd.DataFrame.from_records([{ 'first': 'Tony'}])])

    • @lofenzeq
      @lofenzeq Год назад +4

      thx so much🐼

    • @prathmeshraut7232
      @prathmeshraut7232 Год назад

      Are bro op

    • @azadorucov8712
      @azadorucov8712 11 месяцев назад +5

      They changed it to (_append) now

    • @jMaRiO1986
      @jMaRiO1986 10 месяцев назад

      Gracias

    • @victorlinde1832
      @victorlinde1832 10 месяцев назад +1

      from_records can be omitted df = pd.concat([df, pd.DataFrame([{'first': 'Tony'}])])

  • @suryatejasunny
    @suryatejasunny 4 года назад +77

    Thank you Brilliant for sponsoring Corey.

  • @halim_Ha
    @halim_Ha 4 года назад +63

    The happiness rises when you get notification from corey

  • @RAJASEKARCMR
    @RAJASEKARCMR 10 месяцев назад +4

    07:56 instead of ' append() ' -> ' _append() ' works (underscore in front)

    • @mahangld2070
      @mahangld2070 20 дней назад

      Many Thanks this comment saved me a lot of time

  • @expat2010
    @expat2010 3 года назад +7

    Aside from the unparalleled level of instruction, the production of these videos is superb. Only one of the 5 commercial courses I have taken was in the same league.

  • @elikent78
    @elikent78 Год назад +6

    Hey Corey. Just wanted to say your videos are the best I've come across yet explaining how Pandas works. Great job. Thank you!

  • @dipayanroy8357
    @dipayanroy8357 4 года назад +23

    Corey,
    Can I request a tutorial series on date time forecasting through machine learning? I've gone various videos and articles, but none of them match your clarity and simple way of explaining complex things. Would really love to see that!

  • @Kralnor
    @Kralnor 4 года назад +5

    Thanks for this excellent video series on Pandas. Your style is much cleaner and easier to follow than by trying to read through the official docs and various Stackoverflow posts.
    I'd really like to see a video on the different df.merge variations, particularly using MultiIndex and handling duplicates.

  • @babatundekelvinifeoluwa2318
    @babatundekelvinifeoluwa2318 4 года назад +7

    Great videos!
    I can proudly, your tutorial are the best online tut on python I've ever come across so far...

  • @akritisstory850
    @akritisstory850 4 года назад +2

    Thank you Brilliant for sponsoring this course. Many people are getting help as more and more data analytics jobs are coming, this video is must.

  • @ZsoltPal23092011
    @ZsoltPal23092011 Год назад

    amazing, thank you for these! aside me banging my head into the table from the mix of syntax styles and asking out loud "how the heck will I remember all these" by where do you need 'inplace' and where not, when do you to pass what, the videos are a godsend!

  • @ciprianp495
    @ciprianp495 4 года назад +1

    Wow, man! With a little bit of help from Google and your videos, I was able to automate an Excel File ... Thank you and keep it going! You rock!

  • @ritikjawalkar2877
    @ritikjawalkar2877 Год назад +73

    For all those who are coding in 2023 .append function has been changed into ._append

  • @questscape
    @questscape Год назад

    The speed of information that flows in is insane. Amazing playlist 🙌

  • @Tolgo_on_youtube
    @Tolgo_on_youtube 2 года назад

    Amazing content, thanks for making this complicated topic intelligible, accessible, and available.

  • @GuilhermeMendesG
    @GuilhermeMendesG 2 года назад

    Corey, you saved my life! Thank you so much for this amazing series. I've learned so much!

  • @BugMagnet
    @BugMagnet 2 года назад +1

    Thank you so much for these easy to understand tutorials. My guess is these will get millions of views once schools discover that programming should be a core subject.

  • @fahadshoaib8735
    @fahadshoaib8735 2 года назад +33

    Adding column 0:43
    Removing column 3:08
    Adding rows to data frame 6:40
    Adding two data frames 9:45
    Removing rows 12:11

  • @cookiemonstercupcake
    @cookiemonstercupcake 3 года назад +1

    Hey, Corey!
    You can simply delete columns by using "del" function.
    For example: del df['full_name']

  • @davebeckham5429
    @davebeckham5429 4 года назад +2

    Great tutorial Corey. - Thanks for sharing.

  • @jumalincoln3108
    @jumalincoln3108 Год назад +1

    Thank you Corey for this amazing video. You have helped me much.

  • @rodrigoramirez8150
    @rodrigoramirez8150 3 года назад

    I am grateful for this video. I really appreciate your sharing with us.

  • @biswasv
    @biswasv 3 года назад

    very well explained in a simple way

  • @sudiptomitra
    @sudiptomitra 3 года назад +2

    At the point of 14:13 of this VDO, I got my query resolved ! Thanks & many likes from me :-)

  • @fhdhejeh63
    @fhdhejeh63 3 года назад +4

    I am sure that I will never forget about the sponsor of this video

  • @nguyenquangred
    @nguyenquangred 3 года назад

    Thank you very much for this tutorial series!

  • @niharjamdar4869
    @niharjamdar4869 3 года назад

    I am addicted to your lectures please upload Numpy tutorials too

  • @saitaro
    @saitaro 4 года назад +2

    I thanked you in the previous video, this comment is just to support channel.

  • @SolvingOptimizationProblems
    @SolvingOptimizationProblems 4 года назад +1

    Great python tutorial. Thank you very much!

  • @farhadalam6974
    @farhadalam6974 6 месяцев назад

    Great lesson. Thanks

  • @parvathirajan.n
    @parvathirajan.n 4 года назад +4

    Thanks for the good video !

  • @roywit7836
    @roywit7836 3 года назад

    Very well and clearly explained thanks :)

  • @janevinokur3341
    @janevinokur3341 4 года назад +8

    Thank you so much!!! You provide the best material I've seen so far, so clear and to the point! I wonder how to use Pandas to analyse and organize multiple data into several sheets in a single CSV or Excel file. Are you planning to share this kind of wisdom? :)

    • @coreyms
      @coreyms  4 года назад +8

      I was definitely going to cover exporting to Excel. I wasn’t necessarily going to show combining multiple DataFrames into Excel, but maybe I can throw something together. Thanks for the suggestion!

  • @GeoPoly55
    @GeoPoly55 4 года назад +2

    You're great Sir thank you

  • @melsaied101
    @melsaied101 Год назад

    Thank you for your efforts

  • @cryp0g00n4
    @cryp0g00n4 2 года назад

    This is a superb series

  • @CapsLock959
    @CapsLock959 4 года назад

    Corey, you are the man!

  • @PrashantKumar-nb5ig
    @PrashantKumar-nb5ig 4 года назад

    Great teaching sir

  • @naveenbagri2009
    @naveenbagri2009 3 года назад

    thanks from my heart to u sir, From India.

  • @ambijat
    @ambijat 4 года назад +1

    very nice and clear

  • @ishanpand3y
    @ishanpand3y 4 года назад +1

    Number 6. Thank you, sir.🧡🤍💚

  • @Nishantkj
    @Nishantkj 4 года назад +2

    thanks for all this hard work , your videos are just amazing ..
    i have one question -
    how to use '.applymap(lower)' if some of values are NaN. as we did here after appending df2.
    thanks

  • @igorvujcic1731
    @igorvujcic1731 3 года назад

    Awesome series!

  • @tickokok1410
    @tickokok1410 2 года назад

    Sir you are great Thank you

  • @BlinkDrive555
    @BlinkDrive555 2 года назад

    It's working like charm. thank you

  • @whiteknight1421
    @whiteknight1421 3 года назад

    First of all thanks for the tutorial
    And this might be late, you might as well mention it in later video (which I had yet to cover)
    I notice that if a change is displayed when typed, it is not set to be inplace/assigned, which needed to be done by assigning it

  • @juanpablozapata3904
    @juanpablozapata3904 2 года назад +3

    append no longer works (at least for me). So you can do it with this instead: "df=pd.concat([df,df2],ignore_index=True)"

  • @TarokhYaghoubi
    @TarokhYaghoubi 2 года назад

    Another great video !

  • @MegaKrishnat
    @MegaKrishnat Месяц назад +1

    If anyone wondering how to do df2 with concat then this is how it goes
    df = pd.concat([df,pd.dataframe(df2)], ignore_index=True) 12:14

  • @mohamedhnfy
    @mohamedhnfy Год назад

    Congrats for 1M subscribers

  • @tajsheikh3534
    @tajsheikh3534 3 года назад

    Thankyou. Covered the topic well. How would I remove a bunch of rows?

  • @chriskeo392
    @chriskeo392 3 года назад

    Thanks brilliant

  • @benjamintitcomb9560
    @benjamintitcomb9560 3 года назад

    Thank you so much

  • @experimentalhypothesis1137
    @experimentalhypothesis1137 4 года назад +1

    would be great to have a machine learning series from you

  • @brod515
    @brod515 3 года назад +1

    @11:49 That's probably because appending is extending the memory required to store the database so it would need to create new memory anyway so it can't do it in place.

  • @Gaming__01Guru
    @Gaming__01Guru Год назад

    For the ones who might get an error while using append method this is because append method has been deprecated in pandas 1.4 and removed from the pandas API entirely in version 2.0 rather you can use concat() method

  • @Coney_island23
    @Coney_island23 2 года назад

    excellent!

  • @YeekyYeeky
    @YeekyYeeky 3 года назад

    Thank you very much

  • @kiraal3619
    @kiraal3619 4 года назад +1

    Thaaaanks a lot :DDD

  • @diprachakraborty7718
    @diprachakraborty7718 4 года назад +1

    Corey will you make videos for Exploratory Data Analysis. Thanks

  • @jackli2530
    @jackli2530 4 года назад

    Hi Corey, your tutorial are great, but it would be even better if you could put lesson index at the beginning instead of middle

  • @pandeyUkislay
    @pandeyUkislay Месяц назад

    Hi @Corey - amazing work this and others! more power to you
    Quick question tho - were you able to get the answer to why some method do not have inplace argument

  • @mitchellfolbe8729
    @mitchellfolbe8729 3 года назад

    If they say you learn from your mistakes, I must have learned a lot. I have typed just about every line wrong in the last 5 videos, missing a " ' "or a "]" or a '.' or a ',' or a capital letter or just misspelling words. I am sure I will continue to do so. Frustratingly fun.

  • @gayratsaidakhmedov5451
    @gayratsaidakhmedov5451 3 месяца назад

    thank you

  • @NandhaKumar-hw2bz
    @NandhaKumar-hw2bz 2 года назад

    Thank you🌹🙏

  • @jagadishs3931
    @jagadishs3931 4 года назад

    for old version of pandas this works
    df.drop(['first', 'last'],axis=1, inplace=True)

  • @tejamarneni
    @tejamarneni 4 года назад

    Hi Corey, I have a quick question. When loading a CSV file using the CSV module, I am getting weird characters while using encoding as utf-8 but when using UTF-8-sig it is working fine. Can you please explain the difference between utf-8 and utf-8-sig?

  • @kameshinipillay4587
    @kameshinipillay4587 Год назад

    Great video again thank you! When I use the drop function, I'm getting a "not found in axis" will you be explaining how the axis works also? :p

  • @gautamjha711
    @gautamjha711 3 года назад

    WE ALL NEED VIDEOS FOR DATA STRUCTURES AND ALGORITHMS IN PYHTONNNNNNNN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @bsdvs23
    @bsdvs23 Год назад

    Could you provide some examples using the DataFrame with concat since the append is being phased out?

  • @junaidmalik9593
    @junaidmalik9593 4 года назад +1

    Hi , to perform drop should i use df.drop(Column =[Col1,col2] or its ok to use df.drop([Col1,Col2,Col3],axis=1).
    hope there is no difference b/w both . please confirm.

  • @the-ghost-in-the-machine1108
    @the-ghost-in-the-machine1108 Год назад

    for the ones getting 0 and 1 values when splitting the full_name; just add .values attribute end of the split line

  • @LB-qr7nv
    @LB-qr7nv 5 месяцев назад

    Probably there is only an inplace-parameter if only the affected data (or not much more) gets changed. Append for example probably uses the numpy append which is not inplace, so all the data must be copied. For drop no time-consuming changes need to be done, just one key and pointer get removed

  • @williambeasley838
    @williambeasley838 2 года назад

    Cory, great tutorial. Question, how would i remove multiple rows? Example if i have 10 different names, and i want to remove 5 of them. I tried putting a comma between names, but nothing happens.

  • @mobileentertainment212
    @mobileentertainment212 2 года назад

    Hi, is there a video that teaches conditional columns? For example create a row that 'True' if concentration > 0.1 and Credit_rating == ['CCC', 'BB']

  • @arpitagec9
    @arpitagec9 3 года назад

    Can you pls tell me how can we use apply() to get the full_name column without using the String concatenation?

  • @seanli75
    @seanli75 3 года назад

    thanks, But I wanted to know if there was a ton of rows (like 1 million rows) how do you rename the one that is when it is labeled 0,1,2,3.

  • @shobhitsadwal756
    @shobhitsadwal756 3 года назад

    hey Corey , thanks for the video , my only problem is that the code
    fil=(df2["first"]=="Steve")
    df2.drop(index=df[fil].index)
    is not working correctly,

  • @parrychen4738
    @parrychen4738 3 года назад

    Does pandas directly support mask or filter based dropping of rows? That would be more convenient.

  • @sajilakl
    @sajilakl 4 года назад

    Can you do a series on Date time series?

  • @kabandasafi9132
    @kabandasafi9132 Год назад

    hi corey how could you add in full name does appended name in our data frame? anyway thank you for the insight it is very reached in content.

  • @JoshuaDHarvey
    @JoshuaDHarvey 4 года назад

    Why does df[df[filt]].index return Int64Index([6], dtype='int64') and not either a series or dataframe of the index as an object?

  • @utsavjaiswal4604
    @utsavjaiswal4604 Год назад +1

    10:00 if youre getting an error saying "append is not a method for DataFrame" use this instead
    new_row = pd.DataFrame([[]],columns= ,new_row],ignore_index=True)

    • @RAJASEKARCMR
      @RAJASEKARCMR 10 месяцев назад

      _append() works (undescore in front)

  • @zijunli8482
    @zijunli8482 Год назад +4

    Thanks Corey--could you make an update video about using pd.conta() rather than frame.append() to add new rows? because fame.append() is going to be removed in the future pandas versions

    • @aguer007
      @aguer007 Год назад

      df = pd.concat([df,pd.DataFrame([{'first': 'Tony'}])])

    • @RAJASEKARCMR
      @RAJASEKARCMR 10 месяцев назад

      append() is now _append()

  • @razorblade413
    @razorblade413 8 месяцев назад

    if i can drop rows using dataframe.loc with conditions why would i use .drop with conditions to drop rows?
    It seems like loc function could replace the drop function, at least when dropping rows based on some conditions. Could somebody knows about this?

  • @Anteater23
    @Anteater23 2 года назад +1

    13:31 Why does it not work when I use df.loc(filt) here instead of df[filt] ?

  • @mariosconstantinou8271
    @mariosconstantinou8271 Год назад

    I am a bit confused as to how the drop method knows which rows to remove when we are using a filter.
    We create a filter and apply it to our df and it returns some boolean values. When we type df.drop(index=df[filt].index) we are basically saying drop the values with the index of our filter? It is working, it removed the rows I wanted but I can't figure out how it's working.
    When I printed filt.index it returned a rangeIndex with 6 values (I had 6 rows), some are false and some are true. But when I print df[filt].index it returns the rows 0 and 1 which is the rows that have the value True. So does .index work different depending on where you apply it?
    In the case of df[filt].index it will return the index where the value is True?

  • @shyamss2338
    @shyamss2338 2 года назад

    How do I use apply method to get the fullname. I created the full name function. How to proceed further?

  • @maheshmmmec
    @maheshmmmec 4 года назад

    How to apply different aggregation function on different columns on same group object.

  • @vivshukla7742
    @vivshukla7742 2 года назад

    How we can update any value in coulmn for example row 4 want to update NaN to some other value ,how we can do that?

  • @aaronzolotor430
    @aaronzolotor430 Год назад +1

    In Pandas 2.0 and above the append method has been replaced with concat for anyone who gets confused or frustrated.

  • @rameshreddy908
    @rameshreddy908 3 года назад

    Thanks, Very nice videos. Do you have any udemy courses on Datascience/ML?

  • @dipinadhikari9549
    @dipinadhikari9549 2 года назад

    I have a dataset that have a column saying titile there are some sentence and I want to delete the row having business and rent in the title so how can I do it?

  • @super-creative-stuff1421
    @super-creative-stuff1421 Год назад +1

    str.split() doesn't spllit on the space character it splits on any whitespace character, which include tabs and linebreaks. I would use str.split() not str.split(" ") just in case the person's name is seperated by a tab for some reason.

  • @manajitdas7619
    @manajitdas7619 3 года назад

    love..

  • @VDMStudio
    @VDMStudio 2 года назад

    i use this code for my case:
    filter = df['tenure'] == 0
    df.drop(index=df[filter].index)
    but how to save it to df ?
    when i use inplace the error appears.
    can you help me?

  • @khusboo4907
    @khusboo4907 3 года назад

    What will be the solution if I want to add new row in a dataframe whose value is from the existing dataframe...like for each row a new row will be created having all integer value added upto 1 and string will be appended with _ new.....can anyone help me out

  • @michaelz6408
    @michaelz6408 4 года назад +2

    Hi Corey this is really great video!!! I just have a question, when we call a function, when should we do: .function() and when should we not using the parentheses like this : .function? Thanks!

  • @bishwajitkhanra5369
    @bishwajitkhanra5369 2 года назад

    How can I remove a row or two based on a condition ? e.g. One of the columns has duplicate values and (1) The last row should be kept and other rows needs to be dropped or (2) The first row should be kept and other rows needs to be dropped.