Rows into columns in a multi-index Pandas DataFrame - python

I have a DataFrame with a multi-index, one of dates and the other numbered from 0 to 1267 as the image shows.
How do I have the index 0-1267 as columns instead of rows and have the dates as the only row index?

Select some column and then use Series.unstack by first level:
df1 = df['CUMULATIVE FRACTION'].unstack(0)
Or if need MultiIndex in columns use DataFrame.unstack:
df2 = df.unstack(0)

Related

How do I delete a row when two dataframes in pandas do not have the same length? - python

I have two dataframes in pandas:
df1 can have 365 or 366 rows
df2 has 366 rows
If df1 and df2 have the same number of rows I want to output "same row size".
If, df1 and df2 don't have the same sizes I want to delete row 59 at df2.
all because of leap year :)
Use:
if len(df1) != len(df2):
df2 = df2.drop(59)
Another idea is remove row with 29. Feb, how do it depends of data.

combining dataframes and adding values on common date index

I have many dataframes with one column (same name in all) whose indexes are date ranges - I want to merge/combine these dataframes into one, summing the values where any dates are common. below is a simplified example
range1 = pd.date_range('2021-10-01','2021-11-01')
range2 = pd.date_range('2021-11-01','2021-12-01')
df1 = pd.DataFrame(np.random.rand(len(range1),1), columns=['value'], index=range1)
df2 = pd.DataFrame(np.random.rand(len(range2),1), columns=['value'], index=range2)
here '2021-11-01' appears in both df1 and df2 with different values
I would like to obtain a single dataframe of 62 rows (32+31-1) where the 2021-11-01 date contains the sum of its values in df1 and df2
We can use pd.concate() on the two dataframes, then df.reset_index() to get a new regular-integer index, rename the date column, and then use df.groupby().sum().
df = pd.concat([df1,df2]) # this gives 63 rows by 1 column, where the column is the values and the dates are the index
df = df.reset_index() # moves the dates to a column, now called 'index', and makes a new integer index
df = df.rename(columns={'index':'Date'}) #renames the column
df.groupby('Date').sum()

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

Fill Pandas Dataframe with exisiting dataframe but retain shape

I have created a Pandas dataframe using:
df = pd.DataFrame(index=np.arange(140), columns=np.arange(20))
Which gives me an empty dataframe with 140 rows and 20 columns.
I have another dataframe with 120 columns and 20 rows, I call it df2. I would like to add these rows to fill df, but still retain the shape of 140x20.
When I use:
newdf = df.append(df2) I get a dataframe with 280 rows and 20 columns.
df.iloc[:len(df2), :] = df2.values
will do the job. As the no. of columns are same so we can safely do this. Other values in df will remain NaNs. This will update the df2 records at the beginning. If you want at the end, similarly, you can do df.iloc[-len(df2):, :] = df2.values

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

Categories

Resources