Convert row to column header for Pandas DataFrame,
In [21]: df = pd.DataFrame([(1,2,3), ('foo','bar','baz'), (4,5,6)])
In [22]: df
Out[22]:
0 1 2
0 1 2 3
1 foo bar baz
2 4 5 6
Set the column labels to equal the values in the 2nd row (index location 1):
In [23]: df.columns = df.iloc[1]
If the index has unique labels, you can drop the 2nd row using:
In [24]: df.drop(df.index[1])
Out[24]:
1 foo bar baz
0 1 2 3
2 4 5 6
If the index is not unique, you could use:
In [133]: df.iloc[pd.RangeIndex(len(df)).drop(1)]
Out[133]:
1 foo bar baz
0 1 2 3
2 4 5 6
Using df.drop(df.index[1])
removes all rows with the same label as the second row. Because non-unique indexes can lead to stumbling blocks (or potential bugs) like this, it's often better to take care that the index is unique (even though Pandas does not require it).
Python Pandas: Convert Rows as Column headers
You're looking for pivot_table
:
In [11]: medals = df.pivot_table('no of medals', ['Year', 'Country'], 'medal')
In [12]: medals
Out[12]:
medal Bronze Gold Silver
Year Country
1896 Afghanistan 3 5 4
Algeria 3 1 2
and if you want to reorder the columns:
In [12]: medals.reindex_axis(['Gold', 'Silver', 'Bronze'], axis=1)
Out[12]:
medal Gold Silver Bronze
Year Country
1896 Afghanistan 5 4 3
Algeria 1 2 3
Convert combinations of row+column as Column headers
Let us do pivot_table
then swaplevel
s = df.pivot_table(index= ['Machine','Time'],
columns = df.Part.astype(str).radd('Part'),
values=['PowerA','PowerB'],
fill_value=-1).swaplevel(1,0, axis=1).sort_index(level=0, axis=1)
s.columns = s.columns.map('_'.join)
s.reset_index(inplace=True)
s
Out[751]:
Machine Time Part1_PowerA Part1_PowerB Part2_PowerA Part2_PowerB
0 1 20:30 0.1 0.4 0.9 0.7
1 1 20:31 0.3 0.1 0.2 0.3
2 2 20:30 0.2 0.5 -1.0 -1.0
3 2 20:31 0.8 0.4 -1.0 -1.0
How to Convert rows into columns headers and values of other column as data?
I think you need convert Name
to index, select by double []
for one column DataFrame
and transpose:
df1 = df.set_index('Name')[['Data']].T
Using Pandas convert column values to column header
I think code below will help you:
# create your processing function
def handle_function(row: pd.Series) -> pd.Series:
"""
What you want to do here...
"""
# get label and value
label = row["field_label"]
value = row["field_value"]
# create column for label if it exists no create
row[label] = value
# return new row
return row
# apply on your dataframe
df.apply(handle_function, axis=1)
# remove columns for label and values
df.drop(["field_label", "field_value", axis=1, inplace=True]
Convert first row of pandas dataframe to column name
I believe need to add parameter to read_html
:
df = pd.read_html(url, header=1)[0]
Or:
df = pd.read_html(url, skiprows=1)[0]
Replacing Header with Top Row
new_header = df.iloc[0] #grab the first row for the header
df = df[1:] #take the data less the header row
df.columns = new_header #set the header row as the df header
How can I create column headers from row values in a slice of columns resulting in the column row values matching the column headers?
The idea is to convert the DataFrame to the long form and then pivot:
df = df.reset_index()
df = pd.melt(df, id_vars=['index']).pivot(index = 'index', values='variable', columns='value')
for c in df.columns:
df.loc[df[c].notna(), c] = c
value bar baz egg foo ham
index
0 bar NaN NaN foo ham
1 bar baz NaN foo NaN
2 NaN baz egg NaN ham
Detailed explanation:
melt
would convert the DataFrame to the following form:
index variable value
0 0 A foo
1 1 A baz
2 2 A ham
3 0 B bar
4 1 B foo
5 2 B baz
6 0 C ham
7 1 C bar
8 2 C egg
Then use pivot to make the columns to be all the unique values:
value bar baz egg foo ham
index
0 B NaN NaN A C
1 C A NaN B NaN
2 NaN B C NaN A
Then simply replace all non-na columns to be the column name.
Related Topics
Python's JSON Module, Converts Int Dictionary Keys to Strings
Python Method for Reading Keypress
How to Make Environment Variable Changes Stick in Python
How to Extract Top-Level Domain Name (Tld) from Url
Python Read from Subprocess Stdout and Stderr Separately While Preserving Order
How to Get a Raw, Compiled SQL Query from a SQLalchemy Expression
Modules Are Installed Using Pip on Osx But Not Found When Importing
Modules Are Installed Using Pip on Osx But Not Found When Importing
Use .Corr to Get the Correlation Between Two Columns
Numpy - Create Matrix with Rows of Vector
High-Precision Clock in Python
Update Row Values Where Certain Condition Is Met in Pandas
Why Is the Global Keyword Not Required in This Case
Python Pandas: How to Specify Data Types When Reading an Excel File
How to Improve the Label Placement in Scatter Plot