iloc index
First establish the following data:
import pandas as pd
df = pd.DataFrame([['乔峰', '男', 95, '降龙十八掌', '主角'],
['虚竹', '男', 93, '天上六阳掌', '主角'],
['段誉', '男', 92, '六脉神剑', '主角'],
['王语嫣', '女', 95,'熟知武诀', '主角'],
['包不同', '男', 65, '胡搅蛮缠', '配角'],
['康敏', '女', 40, '惑夫妒人', '配角']],
index=list('abcdef'.upper()),
columns=['name', 'gender', 'score', 'skill', 'class'])
df
The results are as follows:
We are now going to index "Wang Yuyan", iloc
how should it be used ?
Count it and find that its coordinates are (3,0).
df.iloc[3,0]
The result is as follows:
So series
how does the data structure use the iloc
index?
#列索引直接得到series
dfn=df["name"]
print(type(dfn))
dfn
series
It iloc
should be noted that since there is only one column, the row label of iloc is written as usual. The column label must be 0, so there is no need to write it. If it is written, an error will be reported!
dfn.iloc[3]
Finally, let me remind you that iloc
the row and column labels are counted in strict order, and have .index
nothing to do with yours , even if the above data is changed .index
from the original [A,B,C,D,E,F] to [5,4, 3,2,1,0],dfn.iloc[0]
is to output "Qiao Feng" instead of "Kang Min".
Modify index
1. Dataframe .index
assignment for overall modification.
index=[i for i in range(df.shape[0])]
df.index=index
df
Note that a single modification will report an error.
print(df.index)
df.index[3]=10
2. Use to rename
achieve a single modification.
df.rename(index={
3:10})
Note: There is no real modification above, the real modification can be:
df.rename(index={
3:10},inplace=True)
#或者
df=df.rename(index={
3:10})
Modifying the content of the columns index is similar to the index.
df.columns