How do I transpose dataframe in pandas without index?
You can set the index to your first column (or in general, the column you want to use as as index) in your dataframe first, then transpose the dataframe. For example if the column you want to use as index is 'Attribute'
, you can do:
df.set_index('Attribute',inplace=True)
df.transpose()
Or
df.set_index('Attribute').T
Transpose DataFrame in Pandas while preserving Index column
If I understand your example, what seems to happen to you is that you transpose
takes your actual index (the 0...n sequence as column headers. First, if you then want to preserve the numerical index, you can store that as id2
.
DF['id2'] = DF.index
Now if you want id
to be the column headers then you must set that as an index, overriding the default one:
DF.set_index('id',inplace=True)
DF.T
I don't have your data reproduced, but this should give you the values of id
across columns.
How do I transpose a pandas dataframe while preserving the index and rename keys
I was able to do it another way. Here's my code. After transposing:
df.columns = df.iloc[0]
df = df.reset_index()
df = df.drop(df.index[0])
df = df.reset_index()
df =df.drop(["level_0","index"],axis = 1)
Properly transpose Pandas dataframe and access first column
Set fruit
as the index; selection should be much easier:
temp = df.set_index('fruit')
temp.loc['weight', 'apple']
50
Pandas: After using transpose old index name does not vanish when using dataFrame.index.set_names
First question. Why is country now the index for 1800, 1801 etc. and is there a better tranpose option to avoid this?
'country' is the name/ label of the column axis since it was the name of the index of the DataFrame before transposing.
df_2.index.set_names(["year"],inplace=True)
Question two. Why is country still there and how to remove it?
Because you are only changing the index name of the transposed DataFrame. The name of the column axis ('country') is left unchanged.
How to remove it:
You can use DataFrame.rename_axis
to change the name of both axes (index and column) at the same time. If you want to remove the name of a given axis, just pass None.
For instance,
# or df_2 = df1.T.rename_axis(index='year', columns=None) if you prefer
>>> df_2 = df_1.rename_axis(index=None, columns='year').T
>>> df_2
Germany
year
1800 38.4
1801 38.4
1802 38.4
Transposing all but the first column in a dataframe
When transposing, the index becomes columns and the columns index, so you need to first set ISIN as the index and then transpose.
import pandas as pd
df = pd.DataFrame({'ISIN': ['A', 'B', 'C'],
'Jan': [40000, 50000, 42000],
'Feb': [40000, 50000, 42000],
'Mar': [40000, 50000, 42000]})
df.set_index('ISIN', inplace=True)
print(df.T)
Which produces:
ISIN A B C
Jan 40000 50000 42000
Feb 40000 50000 42000
Mar 40000 50000 42000
Related Topics
How to Declare an Array in Python
How to Distribute Python Programs
How to Share Conda Environments Across Platforms
Python Selenium Chrome Webdriver
Update Tkinter Label from Variable
Pandas: Filtering Multiple Conditions
What Are Good Uses for Python3's "Function Annotations"
What's the Shortest Way to Count the Number of Items in a Generator/Iterator
Scrollbar on Matplotlib Showing Page
Defining Private Module Functions in Python
Matplotlib: Overlay Plots with Different Scales
How to Create a Read-Only Class Property in Python
How to Select Python Version in Pycharm
How Can Strings Be Concatenated
How to Delete All Blank Lines in the File with the Help of Python