I want to add data of first row of the dataframe to its column name & delete first row.
Source DataFrame:
2013K2 2013K3 2013K4 2013K5
ABC1 ABC2 ABC3 ABC4
324 5435 543 543
6543 543 657 765
765 876 876 9876
Need to rename column name as Column Name +'|'+ Data of First row:
2013K2|ABC1 2013K3|ABC2 2013K4|ABC3 2013K5|ABC4
324 5435 543 543
6543 543 657 765
765 876 876 9876
Use DataFrame. You can use df. columns=df. iloc[0] to set the column labels by extracting the first row. In pandas, the index starts from 0 hence 0 means first row.
By use + operator simply you can concatenate two or multiple text/string columns in pandas DataFrame.
Get the First Row of a Particular Column in DataFrame Using Series. loc() To get a particular row from a Series object using Series. loc() , we simply pass the row's index name as an argument to the Series.
Select & print first row of dataframe using head() It will return the first row of dataframe as a dataframe object. Using the head() function, we fetched the first row of dataframe as a dataframe and then just printed it.
With concatenation, your datasets are just stitched together along an axis — either the row axis or column axis. Visually, a concatenation with no parameters along rows would look like this: To implement this in code, you’ll use concat () and pass it a list of DataFrames that you want to concatenate. Code for this task would like like this:
Example 2: Similarly, we can concatenate any number of columns in a dataframe. Let’s see through another example to concatenate three different columns of the day, month, and year in a single column Date.
If you want to join on columns like you would with merge(), then you’ll need to set the columns as indices. Like merge(), .join() has a few parameters that give you more flexibility in your joins. However, with .join(), the list of parameters is relatively short: other: This is the only required parameter. It defines the other DataFrame to join.
For pandas.DataFrame, both join and merge operates on columns and rename the common columns using the given suffix. In terms of row-wise alignment, merge provides more flexible control. Different from join and merge, concat can operate on columns or rows, depending on the given axis, and no renaming is performed.
IIUC
df.columns=df.columns+'|'+df.iloc[0,:]
df.iloc[1:,]
Out[41]:
0 2013K2|ABC1 2013K3|ABC2 2013K4|ABC3 2013K5|ABC4
1 324 5435 543 543
2 6543 543 657 765
3 765 876 876 9876
df=df.iloc[1:,]
df
Out[43]:
0 2013K2|ABC1 2013K3|ABC2 2013K4|ABC3 2013K5|ABC4
1 324 5435 543 543
2 6543 543 657 765
3 765 876 876 9876
You could do it like this, using T
and set_index
, then combining multiindex columns to a single column heading using map
and format
.
df_out = df.T.set_index(0, append=True).T
df_out.columns = df_out.columns.map('{0[0]}|{0[1]}'.format)
df_out
Output:
2013K2|ABC1 2013K3|ABC2 2013K4|ABC3 2013K5|ABC4
1 324 5435 543 543
2 6543 543 657 765
3 765 876 876 9876
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With