This question already has answers here:
How to switch columns rows in a pandas dataframe
(2 answers)
How can I pivot a dataframe?
(5 answers)
Closed 4 years ago.
I am trying to pivot the below dataframe. I want the column names to be added as rows. First row is a statis one but the Column names are not static since they will be calculated for the all numerical columns from the data frame. Could you please help.
This is my data frame:
Expected Dataframe:
You just add .T :) df.describe().T to transpose your results:
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
'Rahul','David','Andrew','Ajay','Teresa']),
'Age':pd.Series([26,27,25,24,31,27,25,33,42,32,51,47]),
'Score':pd.Series([89,87,67,55,47,72,76,79,44,92,99,69])}
#Create a DataFrame
pd.DataFrame(d).describe().T
Results:
You can transpose the dataframe:
data_pivot = data_pd.T
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.transpose.html
Other way is .transpose():
data_pivot = data_pd.transpose()
Related
This question already has answers here:
Use a list of values to select rows from a Pandas dataframe
(8 answers)
Closed 10 months ago.
I have a pandas dataframe containing data and a python list which contains ids. I want to extract data from the pandas dataframe which matches with the values of list.
ids = ['SW00003062', 'SW00003063', 'SW00003067', 'SW00003072']
Dataframe is this:
You can use isin
out = df[df['id'].isin(ids)]
This question already has answers here:
How do I find numeric columns in Pandas?
(13 answers)
Closed 12 months ago.
How do we select the columns which have numeric values of all the columns present in a data frame?
We can select the required columns by using the column name and then by slicing those columns from the data frame, but how do we extract those columns using the data type that is present in it
let's imagine you already have a dataframe df
df with only numerical data is
import pandas as pd
import numpy as np
df_numeric = df.select_dtypes(include=np.number)
This question already has answers here:
How to remove nan value while combining two column in Panda Data frame?
(5 answers)
Closed 1 year ago.
I have two DFs that I am trying to merge on the column 'conId'.The DFs have different number of rows and the only other overlapping column is 'delta'.
I am using pf.merge(greek,on='conId',how='left')
The resulting DF is giving me columns 'delta_x' and 'delta_y'
how can I merge these two columns into one column?
Thank you!
You can use
df['delta_x'] = df['detlt_x'].fillna(df['delta_y'])
then drop column if you want
df.drop(['delta_y'], axis=1)
This question already has answers here:
transform pandas pivot table to regular dataframe
(3 answers)
Closed 1 year ago.
Hi I am using pandas pivot functionality .
df1=pd.pivot_table(df, index=[week_column,'Rank_Trx'],columns='Target channel',values= [id_column],aggfunc=pd.Series.nunique)
The output I receive is something like this.
However the output i am looking for should look like this.
So the columns of the data frame I wish to have should be "Period","Rank_Trx","PDE","Samples","Take no action"
Basically I want this a NxN data frame without any multilevel index . I am using the latest pandas version 1.0.5
You can reset the index of the dataframe with
df1 = df1.reset_index()
This question already has answers here:
Pandas: sum DataFrame rows for given columns
(8 answers)
Closed 4 years ago.
I want to sum multiple columns of dataframe to a new column. For 2 columns I was using this.
import pandas as pd, numpy as np
df=pd.read_csv("Calculation_test.csv")
#creating new colums
df["Test1"] = 0
#sum of 2 columns
df["Test1"]= df['col1']+df['col2']
df.to_csv('test_cal.csv', index=False)
But, for my project, I need to do sums of around 15-20 columns. Every time I do not want to write df['col1']+df['col2']+......................
I have the list of columns, which I have to add. Like:
'col1'+'col2'+ 'col5'+'col8'+----+'col18'
or like this:
'col1', 'col2', 'col5', 'col8',----,'col18'
How can I use this list directly to do the sum of columns?
Try slicing the columns:
import pandas as pd
df = pd.read_csv("whatever.csv")
df.loc[:,'col1':'col18'].sum(axis = 1)