pandas date_range dataframe to column headers [duplicate] - python

This question already has answers here:
How to switch columns rows in a pandas dataframe
(2 answers)
Closed 7 months ago.
Im new to python, and im attempting to create a date_range, convert the date_range to a DataFrame and convert each DataFrame row into a header. I have been perusing through the interwebs and cannot find a solution. It seems a simple problem, but i guess im to new to implement a simple solution. Any help is apreciated.
Here is what i have:
duration = pd.date_range(start='1/1/2022', periods=52, freq='W')
df = pd.DataFrame({'Date': duration})
Result:
RESULT
Need code for desired result:
Expected Result

Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. The property T is an accessor to the method transpose().

Related

split a column to two column with duplicates on the ID using python [duplicate]

This question already has answers here:
Max and Min date in pandas groupby
(3 answers)
Closed 4 months ago.
enter image description here
Hello, does someone have an idea to help to split a column in two columns. I have duplicates in my Id columns so i have some difficulties if someone can lay me some track, i would appreciate a lot.
there is a png to illustrate my situation
You can use the groupby function and then aggregate the values.
df = df.groupby('Id').agg({'Date': ['min', 'max']}).reset_index()

Reverse explode with dummy df [duplicate]

This question already has answers here:
Reversing 'one-hot' encoding in Pandas
(9 answers)
Closed 1 year ago.
I've been trying to use reverse explode from here: How to implode(reverse of pandas explode) based on a column
But I have a little bit different df.
I have df looking like this:
I need to 'reverse explode' it, but I couldn't find any option to groupby by index. Is there any option to do that?
To be precise, I need all columns to remain, but all '1' should be combined in a row.
I merged dummy df with main df, but can not figure out what to do next.
rest_cuisine_style = pd.concat([rest_cuisine_style, cuisine_dummies], axis=1)
Does this work?
rest_cuisine_style = rest_cuisine_style.idxmax(axis=1)

How to set a new index [duplicate]

This question already has answers here:
How to convert index of a pandas dataframe into a column
(9 answers)
Closed 1 year ago.
My df has the columns 'Country' and 'Country Code' as the current index. How can I remove this index and create a new one that just counts the rows? I´ll leave the picture of how it´s looking. All I want to do is add a new index next to Country. Thanks a lot!
If you are using a pandas DataFrame and your DataFrame is called df:
df = df.reset_index(drop=False)

Pandas Pivot - remove multi level index and set as columns [duplicate]

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()

Pandas: Return a new Dataframe with specific non continuous column selection [duplicate]

This question already has answers here:
How to take column-slices of dataframe in pandas
(11 answers)
Closed 6 years ago.
I have a dataframe with 85 columns and something like 10.000 rows.
The first column is Shrt_Desc and the last Refuse_Pct
The new data frame that I want has to have Shrt_Desc, then leave some columns out and then include in series Fiber_TD_(g) to Refuse_Pct
I use:
dfi_3 = food_info.loc[:, ['Shrt_Desc', 'Fiber_TD_(g)':'Refuse_Pct']]
but it gives a syntax error.
Any ideas how can I achieve this?
Thank you.
Borrowing the main idea from this answer:
pd.concat([food_info['Shrt_Desc'], food_info.ix[:, 'Fiber_TD_(g)':]], axis=1)

Categories

Resources