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 a data frame
data <- as.data.frame(t(data))
This ensures that the numeric values are not converted to string values.
Transpose dataframe at certain number of rows
You could use pivot
to reshape. Then use rename_axis
+ reset_index
to get it in the desired form:
out = df.pivot(['Col1', 'Col2'], 'Col3', 'Col4').add_prefix('Col4_').rename_axis(columns=[None]).reset_index()
Output:
Col1 Col2 Col4_1 Col4_2 Col4_3
0 A01 CY5 -1.091990 7.402931 0.089915
1 A01 TEX -10.525215 -38.686164 1.337128
2 A02 CY5 -1.526202 2.229630 -0.604621
3 A06 CY5 8.216859 6.890880 0.466027
Transpose columns to rows in R dataframe
We may use pivot_longer
library(tidyr)
pivot_longer(df, cols = starts_with('cheese'),
names_to = c("product", ".value"), names_sep = "\\.",
values_transform = list(kg = as.integer))
-output
# A tibble: 6 × 4
level1 column.other product kg
<chr> <chr> <chr> <int>
1 A yes cheese1 58
2 A yes cheese2 11
3 B yes cheese1 63
4 B yes cheese2 22
5 C yes cheese1 33
6 C yes cheese2 20
Transpose columns pandas dataframe
use pd.melt to flatten the dataframe and then sort and remove the unwanted columns
df.drop(columns='ID').melt(id_vars='Name', value_name='Locator').sort_values('Name').drop(columns='variable')
Name Locator
0 DASS ARGENTINA SRL info@iricresanluis.com.ar
2 DASS ARGENTINA SRL 2664941642
4 DASS ARGENTINA SRL 2664941644
6 DASS ARGENTINA SRL info@iricresanluis.com.ar
8 DASS ARGENTINA SRL info_tec@iricresanluis.com.ar
10 DASS ARGENTINA SRL 115456789
12 DASS ARGENTINA SRL contact_mail@gmail.com
1 PEPSI mail_general@pepsi.com
3 PEPSI 456535365
5 PEPSI 7766554399
7 PEPSI mail1@pepsi.com
9 PEPSI mail2@pepsi.com
11 PEPSI 8864545332
13 PEPSI last_mail@pepsi.com
Related Topics
Change Size of Axes Title and Labels in Ggplot2
How to Play Birthday Music Using R
Shift Legend into Empty Facets of a Faceted Plot in Ggplot2
Convert a Row of a Data Frame to Vector
Get the Path of Current Script
How to Increase the Size of Points in Legend of Ggplot2
Given a Set of Random Numbers Drawn from a Continuous Univariate Distribution, Find the Distribution
Importing Data into R from Google Spreadsheet
What Are 'User' and 'System' Times Measuring in R System.Time(Exp) Output
How to Convert a Huge List-Of-Vector to a Matrix More Efficiently
How to Do a Data.Table Merge Operation
In Ggplot2, How to Add Additional Legend