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
Question [Solved] Formatting cells of a pandas dataframe into an OpenDocument ods spreadsheet Calab 1 781 Mar-01-2025, 04:51 AM
Last Post: Calab
  Find duplicates in a pandas dataframe list column on other rows Calab 2 2,330 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,679 Aug-26-2024, 04:52 PM
Last Post: Calab
  Add NER output to pandas dataframe dg3000 0 1,192 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 2,768 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 2,061 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 2,106 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 3,487 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,956 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 2,703 Jan-10-2022, 07:19 PM
Last Post: moduki1

Forum Jump:

User Panel Messages

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