How to drop rows when there is a length mismatch?

In summary, the person is having trouble matching the size of a series to a dataframe and is unsure how to proceed. They have asked multiple similar questions but are having difficulty finding a comprehensive resource for working with python dataframes.f
  • #1
53
6
I am trying to drop the series size so that it matches the size of the dataframe as I need to copy the index value of the series into the df but I am getting mismatch errors. df has a size of 100 but time has a size of 200 so I want to remove the extra rows in time to match df. how can i handle this situation?

Python:
df.index = pd.to_datetime(time).values

I have started with:

Python:
if len(df) != len(time):

then I am not sure how best to proceed.
 
  • #2
This is your eleventh thread on more or less the same topic. This seems very inefficient to me, and maybe to you as well. Perhaps it would be more efficient for you to describe your background and what you are trying to do and then someone could recommend a book that might answer your questions all at once.
 
  • Like
Likes berkeman, anorlunda and pbuk
  • #3
Hi, I don't believe my questions were similar as I am dealing with a different dataframe at every point with specific problems that I cannot expect in advance. I am learning while working on these different datasets but I realize that I may be asking too many questions here and to actually have my posts counted seems like I am causing an annoyance. Please also note that I only post questions here after doing an exhaustive search for a solution online but sometimes the problem is very specific that it is difficult to find something close. Anyways, I will refrain from asking for any help here moving forward as I don't want to appear as someone just looking for solutions without trying which is not the case in my situation. Thanks and all the best.
 
  • #4
Hi, I don't believe my questions were similar as I am dealing with a different dataframe at every point with specific problems that I cannot expect in advance.
But many of them were about a dataframe, so it's probably not relevant that you were dealing with different dataframes. I'm inclined to agree with @Vanadium50 that this is probably not the most effecient strategy.

A search I did on "python dataframe" brought up a page named Pandas.dataframe. I don't know if that's the package you're working with, but if so, this appears to be a package of extensions to Python. To use such a package without having a good working knowledge of Python constructs like arrays, dictionaries, and the like, and how to manipulate these structures is IMO the wrong way to go about things.

Knowing your programming background and what you're trying to do, as Vanadium50 asked, would go a long way toward pointing you in a more productive direction, as opposed to answering one-off questions.
 
  • Like
Likes pbuk, hmmm27 and berkeman
  • #5
Yes, I agree that it is inefficient to keep posting issues as it appears and I am new to Python. The datasets that I am dealing with are often formatted in different ways so I have to be comfortable with being able to manipulate them according to my needs. I have done a few courses but I find that sometimes just knowing the concepts is not enough until actually applying them which is what I am trying to do now. However I would appreciate advice on where I can get more references on dealing with mainly python dataframes. thanks.
 
  • #6
However I would appreciate advice on where I can get more references on dealing with mainly python dataframes.
AFAIK, dataframes aren't part of Python. Several of the links I've found mention pandas, a library built on Python. Do a search for "python dataframe" and you'll get a bunch of hits, including documentation, tutorials, and so on.

Here's one that might be helpful -- http://www.gregreda.com/2013/10/26/working-with-pandas-dataframes/
 
  • Like
Likes jim mcnamara

Suggested for: How to drop rows when there is a length mismatch?

Replies
1
Views
707
Replies
2
Views
549
Replies
1
Views
473
Replies
1
Views
924
Replies
2
Views
176
Replies
2
Views
522
Replies
2
Views
706
Back
Top