Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat column name with data of first row, Python 3.6 Dataframe

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
like image 757
Learnings Avatar asked Dec 28 '17 15:12

Learnings


People also ask

How do I make the first row a column name in Python?

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.

How do you concatenate columns in a Dataframe in Python?

By use + operator simply you can concatenate two or multiple text/string columns in pandas DataFrame.

How do I get the first row and first column in Python?

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.

How do I get the first row value of a Dataframe?

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.

How do I concatenate DataFrames in Python?

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:

How many columns can be concatenated in a Dataframe?

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.

How do I join two Dataframe columns in Python?

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.

What is the difference between join and concat in pandas?

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.


2 Answers

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
like image 105
BENY Avatar answered Sep 28 '22 00:09

BENY


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
like image 31
Scott Boston Avatar answered Sep 28 '22 01:09

Scott Boston