How do I change a single index value in pandas dataframe?
You want to do something like this:
as_list = df.index.tolist()
idx = as_list.index('Republic of Korea')
as_list[idx] = 'South Korea'
df.index = as_list
Basically, you get the index as a list, change that one element, and the replace the existing index.
How to convert index of a pandas dataframe into a column
either:
df['index1'] = df.index
or, .reset_index
:
df = df.reset_index(level=0)
so, if you have a multi-index frame with 3 levels of index, like:
>>> df
val
tick tag obs
2016-02-26 C 2 0.0139
2016-02-27 A 2 0.5577
2016-02-28 C 6 0.0303
and you want to convert the 1st (tick
) and 3rd (obs
) levels in the index into columns, you would do:
>>> df.reset_index(level=['tick', 'obs'])
tick obs val
tag
C 2016-02-26 2 0.0139
A 2016-02-27 2 0.5577
C 2016-02-28 6 0.0303
Change number format in dataframe index
You can convert your indexes values to int
, but you must do it carefully, cause you can loose some ids:
df = pd.DataFrame({'PaperId':[1000000000.0, 2.0, 3.0, 4.0],
'memberNum':[1, 2, 3, 4]})
df = df.set_index('PaperId')
df
memberNum
PaperId
1.000000e+09 1
2.000000e+00 2
3.000000e+00 3
4.000000e+00 4
df['PaperId'] = df.index
df['PaperId'] = df['PaperId'].astype('int')
df = df.set_index('PaperId')
df
memberNum
PaperId
1000000000 1
2 2
3 3
4 4
Change the index number of a 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)
Pandas - Replace values based on index
Use loc
:
df.loc[0:15,'A'] = 16
print (df)
A B
0 16 45
1 16 5
2 16 97
3 16 58
4 16 26
5 16 87
6 16 51
7 16 17
8 16 39
9 16 73
10 16 94
11 16 69
12 16 57
13 16 24
14 16 43
15 16 77
16 41 0
17 3 21
18 0 98
19 45 39
20 66 62
21 8 53
22 69 47
23 48 53
Solution with ix
is deprecated.
python: changing row index of pandas data frame
you can do
followers_df.index = range(20)
Is there an easier way to change the index values of a pandas dataframe?
try:
test = test.reset_index(drop=True).rename_axis('row_id')
Set value for particular cell in pandas DataFrame using index
RukTech's answer, df.set_value('C', 'x', 10)
, is far and away faster than the options I've suggested below. However, it has been slated for deprecation.
Going forward, the recommended method is .iat/.at
.
Why df.xs('C')['x']=10
does not work:
df.xs('C')
by default, returns a new dataframe with a copy of the data, so
df.xs('C')['x']=10
modifies this new dataframe only.
df['x']
returns a view of the df
dataframe, so
df['x']['C'] = 10
modifies df
itself.
Warning: It is sometimes difficult to predict if an operation returns a copy or a view. For this reason the docs recommend avoiding assignments with "chained indexing".
So the recommended alternative is
df.at['C', 'x'] = 10
which does modify df
.
In [18]: %timeit df.set_value('C', 'x', 10)
100000 loops, best of 3: 2.9 µs per loop
In [20]: %timeit df['x']['C'] = 10
100000 loops, best of 3: 6.31 µs per loop
In [81]: %timeit df.at['C', 'x'] = 10
100000 loops, best of 3: 9.2 µs per loop
Change all index values of pandas dataframe to a range
You can convert your index to a pandas Categorical and then substitute your index by the categorical's codes:
df.index = pd.Categorical(df.index).codes
Related Topics
Assign Headers Based on Existing Row in Dataframe in R
R Plot Color Combinations That Are Colorblind Accessible
How to Separate Title Page and Table of Content Page from Knitr Rmarkdown PDF
Convert Ggplot Object to Plotly in Shiny Application
Defining Minimum Point Size in Ggplot2 - Geom_Point
Add Text to Geom_Line in Ggplot
How to Get My Blogdown Blog on R-Bloggers
Move a Column to First Position in a Data Frame
How to Apply Function Over Each Matrix Element's Indices
R Package Xtable, How to Create a Latextable with Multiple Rows and Columns from R
What's the Difference Between Hex Code (\X) and Unicode (\U) Chars