How to merge multiple columns into a single column? - python

I have a dataframe of apartment flats.
E.g. of columns: 1_room_flat, 2_room_flat, 3_room_flat.
How do i merge all 3 features into one with just "Flat_Type"?

Related

Melt multiple columns in large dataframe in python

I have a dataframe with 78 columns, but i want to melt just 10 consecutives. Is there any way to select that columns range and leave others just like they are?

Python join dataframes by index

I'm working with multiple dataframes in Python and I'm looking to map one onto the other based on a common column (similar to index/match in Excel). I want to join the date column of one dataframe, to the index of the other dataframe (where the date is stored as the index). How would I call out the index? For reference, I want to subtract my ROI for dataframe 2 (awk_price) to the ROI from dataframe 1 (S&P 500). The dataframes are shown below.
I currently have a merged dataframe using
pd.merge(awk_price,sp_500, left_index=True, right_on='Date')
I would love to just add a column to df2 subtracting ROI from dataframe 2 by ROI from dataframe 1 but I can't figure out how to "map" the dates column from dataframe 1 to the index from dataframe 2.
Dataframe 2 (awk_price)
Dataframe 1 (sp_500)
You can use reset_index(), and then rename the column:
df=df1.reset_index().rename(columns={"index": "Date"})
df

Copying dataframes columns into another dataframe

I have two dataframes df1 and df2 where df1 has 9 columns and df2 has 8 columns. I want to replace the first 8 columns of df1 with that of df2. How can this be done? I tried with iloc but not able to succeed.
Following are the files:
https://www.filehosting.org/file/details/842516/tpkA0t2vAtkrqKTb/df1.csv for df1
https://www.filehosting.org/file/details/842517/8XpizwCAX79p9rrZ/df2.csv for df2
import pandas as pd
df1=pd.DataFrame({0:[1,1,1,0,0,0],1:[0,1,0,0,0,0],2:[1,1,1,0,0,0],3:[0,0,0,2,3,4],4:[0,0,0,0,1,0],5:[0,0,0,2,1,2]})
df2=pd.DataFrame({6:[2,2,2,0,0,0],7:[0,2,0,0,0,0],8:[2,2,2,0,0,0],'d':[0,0,0,2,3,4],'e':[0,0,0,0,1,0],'f':[0,0,0,2,1,2]})
z=pd.concat([df1.iloc[:,3:],df2.iloc[:,0:3]],axis=1)
Here I have concatenated from 3rd column to last column of 1st dataframe and the first 3 column of 2nd dataframe. Similarly you concatenate whichever row or column you want to concatenate

Find Mismatched Data Between Two Different Dataframe Columns

I am trying to compare two columns, both from different dataframes. The issue is that these columns are not in the same order.
Given the two dataframes below:
df1
index Letter
1 C,D,X
2 E,F
3 A,B
df2
index Letter
1 A
2 C,D
3 F
I want to identify all data that these two dataframes do not have in common. For example, one dataframe has 'A,B' while the other one has only 'A'. I am trying to flag the missing 'B' in df2. Would the best approach be to split each row of 'Letter' into a list of lists and then create a dictionary to compare?

DataFrame merge on column gives NaN

I have two DataFrames with the first df:
indegree interrupts Subject
1 2 Weather
2 3 Weather
4 5 Weather
The second join:
Subject interrupts_mean indegree_mean
weather 2 3
But the second is a lot shorter since I made that the means of all the different subjects in the first dataframe.
When I want to merge both DataFrames
pd.merge(df,join,left_index=True,right_index=True,how='left')
it merges but it gives NaNs on the second dataframe in the new dataframe and I suppose it it so since the DataFrames are not the same length. How can I still merge on subject so that the values from the second DataFrame are duplicated in the new DataFrame?

Categories

Resources