How to set a new index [duplicate] - python

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)

Related

Iterate over rows/columns and extract values to a different dataframe [duplicate]

This question already has answers here:
Pandas Merging 101
(8 answers)
Closed 5 months ago.
df
df1
dfsum
using df-column'code', i want to reference to df1 and return column 'title' & 'cu' values to dfsum
if both df have the same size the you can iterate just like a regular matrix
# go through the rows
for row in range(total_rows):
# go through the columns
for column in range(total_columns):
#make the condition if they match
if df[row][column] == df1[row][column]:
# now just assign the value from df1 to df
df[row][column] = df1[row][column]
i hope this solves your issue :)

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)

convert specific columns values to column_names in pandas [duplicate]

This question already has answers here:
How can I pivot a dataframe?
(5 answers)
How to pivot a dataframe in Pandas? [duplicate]
(2 answers)
Closed 1 year ago.
I have a dataframe like this:
index,col1,value
1,A,1
1,B,2
2,A,3
2,D,4
2,C,5
2,B,6
And I would like to convert this dataframe to this:
index,col1_A,col1_B,col1_C,col1_D
1,1,2,np.Nan,np.nan
2,3,4,5,6
The conversion is based on the index column value and for each unique index column, the column values from col1 is converted to column name and its associated value is set to the corresponding value available in value columns.
Currently my solution contains looping by creating subset of df as temporary df based on each index and then starting looping there. I am wondering if there is already builtin solution available for it in pandas. please feel free to suggest.

Append only last row in a panda dataframe to a new dataframe [duplicate]

This question already has answers here:
How to get the last N rows of a pandas DataFrame?
(3 answers)
Closed 2 years ago.
I have the following panda dataframe:(df)
How can I append only the last row (Date 2021-01-22) to a new dataframe (df_new)?
df_new = df_new.append(df.tail(1))
if df_new is not defined. The following code will do it.
df_new = df.tail(1)

Loosing the column name with getting a subset of a dataframe [duplicate]

This question already has answers here:
Keep selected column as DataFrame instead of Series
(5 answers)
Closed 5 years ago.
I'm going to get a subset of my dataframe, but it's column name disappears. This is the code I use:
my_dataframe = df["column1"]
How can have the subset of df in my_dataframe without loosing the column name?
This gives a Series:
my_series = df["column1"]
but this a sub-dataframe:
my_dataframe = df[["column1"]]
which shows the column name.

Categories

Resources