After I'm done with some manipulation in Dataframe, I got a result dataframe. But the index are not listed properly as below.
MsgType/Cxr NoOfMsgs AvgElpsdTime(ms) 161 AM 86 30.13 171 CM 1 104 18 CO 27 1244.81 19 US 23 1369.61 20 VK 2 245 21 VS 11 1273.82 112 fqa 78 1752.22 24 SN 78 1752.22
I would like to get the result as like below.
MsgType/Cxr NoOfMsgs AvgElpsdTime(ms) 1 AM 86 30.13 2 CM 1 104 3 CO 27 1244.81 4 US 23 1369.61 5 VK 2 245 6 VS 11 1273.82 7 fqa 78 1752.22 8 SN 78 1752.22
Please guide how I can get this ?
You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.
To set the DataFrame index using existing columns or arrays in Pandas, use the set_index() method. The set_index() function sets the DataFrame index using existing columns. The index can replace the existing index or expand on it.
In order to set index to column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column. If you are not aware by default, pandas adds an index to each row of the pandas DataFrame.
If we didn’t specify index values to the DataFrame while creation then it will take default values i.e. numbers starting from 0 to n-1 where n indicates a number of rows. To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes.
In Python, we can easily set any existing column or columns of a Pandas DataFrame object as its index in the following ways. 1. Set column as the index (without keeping the column) In this method, we will make use of the inplace parameter which is an optional parameter of the set_index () function of the Python Pandas module.
So to reset the index to the default integer index beginning at 0, We can simply use the reset_index() function. So let’s see the different ways we can reset the index of a DataFrame. First see original DataFrame.
Similarly, we can rename a multi-index dataframe indices by assigning a list to the .names attribute. Let’s say we want to name them: ‘Time Period’ and ‘Average Sales’: This returns the following dataframe: In this section, you learned how to rename the indices of a multi-index dataframe.
These are the rownames
of your dataframe, which by default are 1:nrow(dfr)
. When you reordered the dataframe, the original rownames are also reordered. To have the rows of the new order listed sequentially, just use:
rownames(dfr) <- 1:nrow(dfr)
Or, simply
rownames(df) <- NULL
gives what you want.
> d <- data.frame(x = LETTERS[1:5], y = letters[1:5])[sample(5, 5), ] > d x y 5 E e 4 D d 3 C c 2 B b 1 A a > rownames(d) <- NULL > d x y 1 E e 2 D d 3 C c 4 B b 5 A a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With