How can I combine two dataframes in Python Pandas?

0 votes

I'm using python pandas data frame , I have an initial data frame say X from which I extracted two data frames:

A = X[X.label == k]

B = X[X.label != k]

And then changed the label in A and B:

A.label = 1
B.label = -1

Now I want to combine A and B, such that when we sample A and B from X, they retain their indexes from X.

Jun 24 in Python by Taj
• 1,040 points

recategorized Jun 24 by Taj 47 views

1 answer to this question.

0 votes


You can use the following methods to combine the data frames:

1) append method

df = A.append(B)

2) pd.concat method

df = pd.concat([A,B])
answered Jun 24 by Shabnam
• 920 points

Related Questions In Python

0 votes
1 answer

How can I compare the content of two files in Python?

Assuming that your file unique.txt just contains ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
0 votes
1 answer

How can I write a program to add two numbers using functions in python?

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20 in Python by Mohammad
• 3,060 points
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,810 points
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 333 views
+4 votes
6 answers
0 votes
1 answer

How to add a new Python interpreter in PyCharm?

Refer to the below screenshots: Then set a ...READ MORE

answered May 30 in Python by Shabnam
• 920 points
0 votes
1 answer

How do I run a set of code for all the requests in Flask?

Hi, You can use dedicated hooks(decorators) called before ...READ MORE

answered Jun 20 in Python by Shabnam
• 920 points