How can i transform this table? [duplicate] - python

This question already has answers here:
How do I melt a pandas dataframe?
(3 answers)
Closed 6 months ago.
I have a table with 300 rows and 200 columns and i need to transform it.
I add an image of the transformation with an example table.
The table above is the original. The one below is the table after the transformation.
I was trying to solve it with excel and Pandas library of Python but i could not solve it.
Any ideas?

You can do a melt after reading excel using pandas:
df = pd.read_excel('your_excel_path')
df = pd.melt(df, id_vars='id', value_vars=['variable_1', 'variable_2'])
then write back to excel
df.to_excel('modified_excel.xlsx')

Related

Pandas column separation using .loc [duplicate]

This question already has answers here:
Selecting multiple columns in a Pandas dataframe
(22 answers)
Closed last month.
I am an amateur user.
I watched many videos but I couldn't figure out this error.
How can I keep PERSON_WGHT, LOS, and IDC_DC_CD_1 as a columns for all rows that is 386816.
If you need to select multip0le columns from all the records then use df[[column_list]].
df_new = df[['PERSON_WGHT', 'LOS', 'IDC_DC_CD_1']]

In pandas, how to turn a dataframe into tidy data? [duplicate]

This question already has answers here:
How do I melt a pandas dataframe?
(3 answers)
Convert columns into rows with Pandas
(6 answers)
Closed last year.
In python and pandas I have a dataframe that I need to turn into tidy data to make charts easier
The original data is like this:
I want to transform into a dataframe, with the transposition of the data and adapting column names:
Please is there a way in python to do this?
Use melt:
out = df.melt('year', var_name='localization', value_name='number_of_tests')
You can also use:
out = df.set_index('year').rename_axis(columns='localization').unstack() \
.rename('number_of_tests').reset_index()

How to query from multiindex dataframe containing data and time as index [duplicate]

This question already has answers here:
selecting from multi-index pandas
(7 answers)
Closed 1 year ago.
Check this image of dataframe
I've posted the picture of dataframe I am working with, I want to pull out data from specific times of a certain date
I've tried
stockdf.loc[("2015-01-01")].loc['09:17:00']
stockdf.loc[("2015-01-01","09:17:00"),:]
Both do not work.
Just try:
stockdf.loc[("2015-01-01", "09:17:00")]
If they're dates:
stockdf.loc[(pd.to_datetime("2015-01-01").date(), pd.to_datetime("09:17:00").time())]

Grouping the data in Pandas [duplicate]

This question already has an answer here:
pandas groupby two columns and summarize by mean
(1 answer)
Closed 3 years ago.
I've the pandas dataframe like below
.
I want to transform this DataFrame into another form like below.
I've tried groupby functionality in pandas.But could not able to achieve the solution. Please help me with suggestions. Thanks inadvance.
e=df.groupby(['Country','City'])['Rating'].mean()
pd.DataFrame(e)
This would look like

how to fetch specific column rang in CSV in python using iloc [duplicate]

This question already has answers here:
How to take column-slices of dataframe in pandas
(11 answers)
Closed 3 years ago.
I have a CSV file which has 4 columns and I want fetch only 2nd and 3rd columns in a array using but I'm not able to fetch , always got some wrong set of data , Here is my code
import pandas as pd
dataset = pd.read_csv('data.csv')
x = dataset.iloc[:1 , :1].values
can anyone help me how to get them ?
I hope the above image answers your questions.Upvote if it does :)

Categories

Resources