Python Forum

Full Version: Pandas DataFrame Concatenate problems
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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