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()
Related
This question already has answers here:
How do I Pandas group-by to get sum?
(11 answers)
Closed last month.
Let's say I have the Python pandas dataframe below, I want to combine the amount paid column based on employee ID.
[dataframe]
Well try:
dataframe.groupby('EMPLOYEE_ID')['AMOUNT_PAID'].sum()
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']]
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')
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())]
This question already has answers here:
drop_duplicates not working in pandas?
(7 answers)
DataFrame.drop_duplicates and DataFrame.drop not removing rows
(2 answers)
Closed 3 years ago.
I've got a DataSet with two columns, one with categorical value (State2), and another (State) that contains the same values only in binary.
I used OneHotEncoding.
import pandas as pd
mydataset = pd.read_csv('fieldprotobackup.binetflow')
mydataset.drop_duplicates(['Proto2','Proto'], keep='first')
mydataset.to_csv('fieldprotobackup.binetflow', columns=['Proto2','Proto'], index=False)
Dataset
I'd like to remove all redundancies from the file. While researching, I found the command df.drop_duplicates, but it's not working for me.
You either need to add the inplace=True parameter, or you need to capture the returned dataframe:
mydataset.drop_duplicates(['Proto2','Proto'], keep='first', inplace=True)
or
no_duplicates = mydataset.drop_duplicates(['Proto2','Proto'], keep='first')
Always a good idea to check the documentation when something isn't working as expected.