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

Поделиться
HTML-код
  • Опубликовано: 27 янв 2025

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

  • @coreyms
    @coreyms  5 лет назад +75

    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 5 лет назад +7

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

    • @y0ngjin
      @y0ngjin 5 лет назад

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

    • @coreyms
      @coreyms  5 лет назад +17

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

    • @kaaaaasraa49563
      @kaaaaasraa49563 5 лет назад +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 5 лет назад +1

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

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

    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'}])])

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

    Thank you Brilliant for sponsoring Corey.

  • @RAJASEKARCMR
    @RAJASEKARCMR Год назад +12

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

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

      Many Thanks this comment saved me a lot of time

    • @notanotherstandup
      @notanotherstandup 4 месяца назад

      Thank you my man!

    • @PeeyushSharma-pc8fc
      @PeeyushSharma-pc8fc Месяц назад

      Well you should not use _append() it is private method and is not meant to be the part of public API, instead use .concat you can find the tutorial for it anywhere.

  • @halim_Ha
    @halim_Ha 5 лет назад +63

    The happiness rises when you get notification from corey

  • @elikent78
    @elikent78 2 года назад +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!

  • @fahadshoaib8735
    @fahadshoaib8735 3 года назад +34

    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

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

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

  • @expat2010
    @expat2010 4 года назад +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.

  • @babatundekelvinifeoluwa2318
    @babatundekelvinifeoluwa2318 5 лет назад +7

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

  • @dipayanroy8357
    @dipayanroy8357 5 лет назад +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.

  • @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.

  • @MegaKrishnat
    @MegaKrishnat 7 месяцев назад +3

    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

  • @BugMagnet
    @BugMagnet 3 года назад +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.

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

    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!

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

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

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

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

  • @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!

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

    Great tutorial Corey. - Thanks for sharing.

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

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

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

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

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

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

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

    @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.

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

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

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

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

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

    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.

  • @janevinokur3341
    @janevinokur3341 5 лет назад +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  5 лет назад +9

      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!

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

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

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

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

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

    very well explained in a simple way

  • @SolvingOptimizationProblems
    @SolvingOptimizationProblems 5 лет назад +1

    Great python tutorial. Thank you very much!

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

    It's working like charm. thank you

  • @parvathirajan.n
    @parvathirajan.n 5 лет назад +4

    Thanks for the good video !

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

    Congrats for 1M subscribers

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

    For 6:33 I have this error: ValueError: Columns must be same length as key
    What can I do, any ideas?

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

      Did you find any solutions?, I still stuck too!

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

    Another great video !

  • @pandeyUkislay
    @pandeyUkislay 7 месяцев назад

    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

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

    This is a superb series

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

    Great lesson. Thanks

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

    Thank you very much for this tutorial series!

  • @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 Год назад

      _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

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

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

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

      append() is now _append()

  • @ravipandey7488
    @ravipandey7488 4 месяца назад

    Hi, is it possible to add the new columns to the beginning of the dataframe?

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

    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?

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

    10:24 but now in the latest version (python 3.9) of python it doesn't give this warning it simply appends.

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

    would be great to have a machine learning series from you

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

    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?

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

    Great teaching sir

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

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

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

    Thank you for your efforts

  • @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?

  • @diprachakraborty7718
    @diprachakraborty7718 5 лет назад +1

    Corey will you make videos for Exploratory Data Analysis. Thanks

  • @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

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

    Corey, you are the man!

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

    Very well and clearly explained thanks :)

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

    Awesome series!

  • @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.

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

    Hi ! df.append does not persist. When doing df after that , the new row is not added and neither is recognized by df.loc [ ] . Pls help??

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

      try this :
      people={
      "first" : ["___"],
      "last" : ["__"],
      "email" : ["__"]
      }
      df2=pd.DataFrame(people)
      df=df.append(df2,ignore_index=True,sort=False)
      print(df)
      you cannot use inplace=True, so u should assign the df.append to a new parameter or the existing one

  • @ambijat
    @ambijat 5 лет назад +1

    very nice and clear

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

    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?

  • @ishanpand3y
    @ishanpand3y 5 лет назад +1

    Number 6. Thank you, sir.🧡🤍💚

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

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

  • @GeoPoly55
    @GeoPoly55 5 лет назад +2

    You're great Sir thank you

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

    10:30 df3 = pd.concat([df,df2], ignore_index=True)
    df3
    append won't work with latest python.

  • @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?

  • @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.

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

    thanks from my heart to u sir, From India.

  • @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)"

  • @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.

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

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

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

    Is there a method where we can transpose the rowand columns

  • @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,

  • @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

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

    When running the code "df.append(df2, ignore_index=True)" I'm getting the AttributeError: 'DataFrame' object has no attribute 'append'. Why is that?

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

    Hey Corey, I have a quick question! Let's say i wanna iterate through my dataframe, using a for loop, but I would want to compare the value of a specific column in the current row, and in the next row. How would I be able to do that?

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

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

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

    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']

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

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

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

    Sir you are great Thank you

  • @Camila-fv9qj
    @Camila-fv9qj 2 года назад

    If i had two coulmns how to add them in one coulmns like from 0 -12 index coulmn 1 the rest is coulmn 2

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

    Thank you🌹🙏

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

    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

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

    Hi Corey, great video, a small question: df.drop(index=df[df["last"]== "Doe"].index), this way works pretty well, but why when i use # filt = df["last"] == "Doe"
    # df.drop(index=df[filt].index), I get empty results just showing the titles of each column? thanks for great work!

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

    the append method for DataFrames has been deprecated in pandas versions 0.20.0 and later.

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

    Thanks man !!!!

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

    Thanks brilliant

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

    Shouldn't we getting an error while using drop because axis = 0 is default value for index and we are not giving axis =1 for column drop

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

    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.

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

    What do I do if I want to delete Doe, Rogers and Stark altogether

  • @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?

  • @tejamarneni
    @tejamarneni 5 лет назад

    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?

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

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

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

    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

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

    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

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

    split method is not working properly even after adding (' ') space between commas.
    df['full_name'].str.split(' ')
    Output:
    0 [CoraySchafer]
    1 [JaneDoe]
    2 [JohnDoe]
    How can I fix this ? Anyone can suggest?

  • @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.

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

    excellent!

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

    Hi Corey. How do I filter out records where a specific column has values NaN

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

      If you want to filter out just those results which have NaN Values then you can use isnull() method df[df['Full Name'].isnull()] and it can only be applied to a specific column. If you want to exclude just the NaN values then either you can use notnull() or contains with attribute na = false like corey did with language column.
      df['Language'].str.contains('Python', na = False).