Python Forum
Pandas DataFrame Concatenate problems
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas DataFrame Concatenate problems
#1
import pandas as pd

df = pd.read_excel('C:\\work\\\\Report1.xlsx', sheet_name = 'data1', nrows=6)


df2 = pd.read_excel('C:\\work\\Report1.xlsx', sheet_name = 'data1', header=7)


df2 = df2[df2["Data"].str.contains("Down*", na=False)]
Hello,

The above code reads in df first 6 rows. df2 reads in the same file but starting at row 7. df2 then looks at row "Data", and finds anything with the word "Down" in it.

I end up with the variables
df DataFrame (6,14)
df2 DataFrame (25543,14)



I want to take df, and df2 and bring them back together to create df3. I want the first 6 rows of df to be at the top, and then df2 begins right at row 7. When I concatenate I end up with 28 columns instead of 14, and df2 header ends up at the top of the file instead of starting at row 7.
Reply
#2
It is better if you provide the data too.You can use the concat() method in pandas to achieve what you are looking to do.Also make sure that the column names are same in both the dataframes.You can also use the merge() function. Have a look at this article to understand better pandas. Also you can refer to this page to know more about joins
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 1,021 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,117 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,430 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,310 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,260 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,794 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,548 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,247 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,444 Jun-22-2021, 07:57 PM
Last Post: snippsat
  Concatenate 3D arrays paul18fr 1 2,643 Apr-09-2021, 02:49 PM
Last Post: paul18fr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020