How to drop rows when there is a length mismatch?

  • Python
  • Thread starter msn009
  • Start date
  • Tags
    python
  • #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.
 

Answers and Replies

  • #2
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
26,354
9,837
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
53
6
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
34,667
6,379
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
53
6
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
34,667
6,379
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

Related Threads on How to drop rows when there is a length mismatch?

  • Last Post
Replies
7
Views
525
Replies
1
Views
675
  • Last Post
Replies
3
Views
198
Replies
5
Views
761
Replies
5
Views
2K
Replies
9
Views
16K
Replies
4
Views
1K
Replies
21
Views
1K
Replies
1
Views
501
Top