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 25, 2019 in Python by Taj
• 1,080 points

recategorized Jun 25, 2019 by Taj 1,035 views

1 answer to this question.

0 votes

Hi,

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 25, 2019 by Shabnam
• 930 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
2,351 views
0 votes
2 answers

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

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,235 views
0 votes
1 answer

How can I optimize this condition so that it takes less time to execute in Python Pandas.

Hello @moli, You can easily optimize this code ...READ MORE

answered Sep 8, 2020 in Python by Niroj
• 82,880 points
314 views
+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,890 points
19,316 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,411 views
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, 2019 in Python by Shabnam
• 930 points
5,121 views
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 21, 2019 in Python by Shabnam
• 930 points
567 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP