python基于pandas设置索引

import pandas as pd

# DateFrame中,index为行索引,columns为列索引
pd.set_option('display.unicode.east_asian_width', True)
s1 = 'H:\pythonProject\COD1.csv'
s2 = pd.read_csv(s1, index_col=0)  # 指定第一列为行索引
print(s2)  # 输出原始数据
###Series结构索引
s3 = pd.read_csv(s1)
print('*****---' * 10)
print(s3)  # 输出原始数据
print('*****---' * 10)
a1 = pd.Series([10, 20, 30], index=list("abc"))
a2 = pd.Series([1, 2, 3], index=list("bcd"))
print(a1)  # 输出原始数据
print(a2)  # 输出原始数据
print(a1 + a2)  # 利用索引实现数据的相加减
##利用reindex重新设置索引。
print('*****---' * 10)
print(a1.reindex([1, 2, 3, 4, 5], fill_value=1))  # 重新设置索引,并用1填充
##DataFrame重新设置索引
s4 = s3.reindex(index=['测试行索引1', '测试行索引2', '测试行索引3', '测试行索引4', '测试行索引5', '测试行索引6', '测试行索引7'],
                columns=['测试列索引1', '测试列索引2', '测试列索引3', '测试列索引4', '测试列索引5', '测试列索引6', '测试列索引7',
                         '测试列索引8', '测试列索引9', '测试列索引10', '测试列索引11', '测试列索引12'])
print('*****---' * 10)
print(s4)  # 输出原始数据
print('*****---' * 10)
print(s3)
print('*****---' * 10)
print(s3.set_index(['COD']))

结果为

H:\pythonProject\venv\Scripts\python.exe H:/pythonProject/main.py
           COD        b1        b2        b3        b4        b5
s1    6.246465  0.033064  0.044745  0.063753  0.046467  0.061651
s2    7.300000  0.032765  0.040027  0.060715  0.047964  0.062193
s3    7.151515  0.034787  0.044034  0.068569  0.047349  0.062583
s4    5.858586  0.038918  0.054270  0.070237  0.049240  0.063075
s5    7.458586  0.037524  0.047527  0.065471  0.046837  0.060580
s6    7.458586  0.044111  0.055397  0.075133  0.052282  0.067838
s7    7.022222  0.043152  0.056629  0.072561  0.052936  0.070106
s8    7.846465  0.044698  0.061596  0.073882  0.053898  0.073508
s9   10.561616  0.042522  0.060696  0.069076  0.051668  0.080740
s10   2.828283  0.048858  0.057816  0.077516  0.056419  0.081748
s11   8.492929  0.041209  0.058360  0.070019  0.053007  0.095129
s12  12.581818  0.046677  0.067138  0.071816  0.052377  0.082932
s11   8.492929  0.041209  0.058360  0.070019  0.053007  0.095129
*****---*****---*****---*****---*****---*****---*****---*****---*****---*****---
   Unnamed: 0        COD        b1        b2        b3        b4        b5
0          s1   6.246465  0.033064  0.044745  0.063753  0.046467  0.061651
1          s2   7.300000  0.032765  0.040027  0.060715  0.047964  0.062193
2          s3   7.151515  0.034787  0.044034  0.068569  0.047349  0.062583
3          s4   5.858586  0.038918  0.054270  0.070237  0.049240  0.063075
4          s5   7.458586  0.037524  0.047527  0.065471  0.046837  0.060580
5          s6   7.458586  0.044111  0.055397  0.075133  0.052282  0.067838
6          s7   7.022222  0.043152  0.056629  0.072561  0.052936  0.070106
7          s8   7.846465  0.044698  0.061596  0.073882  0.053898  0.073508
8          s9  10.561616  0.042522  0.060696  0.069076  0.051668  0.080740
9         s10   2.828283  0.048858  0.057816  0.077516  0.056419  0.081748
10        s11   8.492929  0.041209  0.058360  0.070019  0.053007  0.095129
11        s12  12.581818  0.046677  0.067138  0.071816  0.052377  0.082932
12        s11   8.492929  0.041209  0.058360  0.070019  0.053007  0.095129
*****---*****---*****---*****---*****---*****---*****---*****---*****---*****---
a    10
b    20
c    30
dtype: int64
b    1
c    2
d    3
dtype: int64
a     NaN
b    21.0
c    32.0
d     NaN
dtype: float64
*****---*****---*****---*****---*****---*****---*****---*****---*****---*****---
1    1
2    1
3    1
4    1
5    1
dtype: int64
*****---*****---*****---*****---*****---*****---*****---*****---*****---*****---
             测试列索引1  测试列索引2  ...  测试列索引11  测试列索引12
测试行索引1          NaN          NaN  ...           NaN           NaN
测试行索引2          NaN          NaN  ...           NaN           NaN
测试行索引3          NaN          NaN  ...           NaN           NaN
测试行索引4          NaN          NaN  ...           NaN           NaN
测试行索引5          NaN          NaN  ...           NaN           NaN
测试行索引6          NaN          NaN  ...           NaN           NaN
测试行索引7          NaN          NaN  ...           NaN           NaN

[7 rows x 12 columns]
*****---*****---*****---*****---*****---*****---*****---*****---*****---*****---
   Unnamed: 0        COD        b1        b2        b3        b4        b5
0          s1   6.246465  0.033064  0.044745  0.063753  0.046467  0.061651
1          s2   7.300000  0.032765  0.040027  0.060715  0.047964  0.062193
2          s3   7.151515  0.034787  0.044034  0.068569  0.047349  0.062583
3          s4   5.858586  0.038918  0.054270  0.070237  0.049240  0.063075
4          s5   7.458586  0.037524  0.047527  0.065471  0.046837  0.060580
5          s6   7.458586  0.044111  0.055397  0.075133  0.052282  0.067838
6          s7   7.022222  0.043152  0.056629  0.072561  0.052936  0.070106
7          s8   7.846465  0.044698  0.061596  0.073882  0.053898  0.073508
8          s9  10.561616  0.042522  0.060696  0.069076  0.051668  0.080740
9         s10   2.828283  0.048858  0.057816  0.077516  0.056419  0.081748
10        s11   8.492929  0.041209  0.058360  0.070019  0.053007  0.095129
11        s12  12.581818  0.046677  0.067138  0.071816  0.052377  0.082932
12        s11   8.492929  0.041209  0.058360  0.070019  0.053007  0.095129
*****---*****---*****---*****---*****---*****---*****---*****---*****---*****---
          Unnamed: 0        b1        b2        b3        b4        b5
COD                                                                   
6.246465          s1  0.033064  0.044745  0.063753  0.046467  0.061651
7.300000          s2  0.032765  0.040027  0.060715  0.047964  0.062193
7.151515          s3  0.034787  0.044034  0.068569  0.047349  0.062583
5.858586          s4  0.038918  0.054270  0.070237  0.049240  0.063075
7.458586          s5  0.037524  0.047527  0.065471  0.046837  0.060580
7.458586          s6  0.044111  0.055397  0.075133  0.052282  0.067838
7.022222          s7  0.043152  0.056629  0.072561  0.052936  0.070106
7.846465          s8  0.044698  0.061596  0.073882  0.053898  0.073508
10.561616         s9  0.042522  0.060696  0.069076  0.051668  0.080740
2.828283         s10  0.048858  0.057816  0.077516  0.056419  0.081748
8.492929         s11  0.041209  0.058360  0.070019  0.053007  0.095129
12.581818        s12  0.046677  0.067138  0.071816  0.052377  0.082932
8.492929         s11  0.041209  0.058360  0.070019  0.053007  0.095129

进程已结束,退出代码为 0

你可能感兴趣的:(python零基础学习,python,开发语言,后端)