使用rename方法重命名Pandas数据框中的列

2024-09-28 17:28:40 发布

您现在位置:Python中文网/ 问答频道 /正文

一直在尝试更改数据框中的列名

In [1]: import pandas as pd
   ...: df=pd.read_csv('winequality-red.csv',sep=';')
   ...: df.head()
Out[1]: 
   fixed acidity  volatile acidity  citric acid  residual sugar  chlorides  \
0            7.4              0.70         0.00             1.9      0.076   
1            7.8              0.88         0.00             2.6      0.098   
2            7.8              0.76         0.04             2.3      0.092   
3           11.2              0.28         0.56             1.9      0.075   
4            7.4              0.70         0.00             1.9      0.076   

   free sulfur dioxide  total sulfur dioxide  density    pH  sulphates  \
0                 11.0                  34.0   0.9978  3.51       0.56   
1                 25.0                  67.0   0.9968  3.20       0.68   
2                 15.0                  54.0   0.9970  3.26       0.65   
3                 17.0                  60.0   0.9980  3.16       0.58   
4                 11.0                  34.0   0.9978  3.51       0.56   

   alcohol  quality  
0      9.4        5  
1      9.8        5  
2      9.8        5  
3      9.8        6  
4      9.4        5   

在rename中使用replace方法时,收到以下错误消息:

In [2]: df.rename(str.replace(' ', '_'),axis='columns',inplace=True)
   ...: df.head()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-3bf0bb1685ed> in <module>
----> 1 df.rename(str.replace(' ', '_'),axis='columns',inplace=True)
      2 df.head()

**TypeError: replace() takes at least 2 arguments (1 given)**

但是,如果更改str.title、str.upper或str.lower的str.replace,则会起作用

In [3]: df.rename(str.title,axis='columns',inplace=True)
   ...: df.head()
Out[3]: 
   Fixed Acidity  Volatile Acidity  Citric Acid  Residual Sugar  Chlorides  \
0            7.4              0.70         0.00             1.9      0.076   
1            7.8              0.88         0.00             2.6      0.098   
2            7.8              0.76         0.04             2.3      0.092   
3           11.2              0.28         0.56             1.9      0.075   
4            7.4              0.70         0.00             1.9      0.076   

   Free Sulfur Dioxide  Total Sulfur Dioxide  Density    Ph  Sulphates  \
0                 11.0                  34.0   0.9978  3.51       0.56   
1                 25.0                  67.0   0.9968  3.20       0.68   
2                 15.0                  54.0   0.9970  3.26       0.65   
3                 17.0                  60.0   0.9980  3.16       0.58   
4                 11.0                  34.0   0.9978  3.51       0.56   

   Alcohol  Quality  
0      9.4        5  
1      9.8        5  
2      9.8        5  
3      9.8        6  
4      9.4        5  

In [4]: df.rename(str.upper,axis='columns',inplace=True)
   ...: df.head()
Out[4]: 
   FIXED ACIDITY  VOLATILE ACIDITY  CITRIC ACID  RESIDUAL SUGAR  CHLORIDES  \
0            7.4              0.70         0.00             1.9      0.076   
1            7.8              0.88         0.00             2.6      0.098   
2            7.8              0.76         0.04             2.3      0.092   
3           11.2              0.28         0.56             1.9      0.075   
4            7.4              0.70         0.00             1.9      0.076   

   FREE SULFUR DIOXIDE  TOTAL SULFUR DIOXIDE  DENSITY    PH  SULPHATES  \
0                 11.0                  34.0   0.9978  3.51       0.56   
1                 25.0                  67.0   0.9968  3.20       0.68   
2                 15.0                  54.0   0.9970  3.26       0.65   
3                 17.0                  60.0   0.9980  3.16       0.58   
4                 11.0                  34.0   0.9978  3.51       0.56   

   ALCOHOL  QUALITY  
0      9.4        5  
1      9.8        5  
2      9.8        5  
3      9.8        6  
4      9.4        5  

In [5]: df.rename(str.lower,axis='columns',inplace=True)
   ...: df.head()
Out[5]: 
   fixed acidity  volatile acidity  citric acid  residual sugar  chlorides  \
0            7.4              0.70         0.00             1.9      0.076   
1            7.8              0.88         0.00             2.6      0.098   
2            7.8              0.76         0.04             2.3      0.092   
3           11.2              0.28         0.56             1.9      0.075   
4            7.4              0.70         0.00             1.9      0.076   

   free sulfur dioxide  total sulfur dioxide  density    ph  sulphates  \
0                 11.0                  34.0   0.9978  3.51       0.56   
1                 25.0                  67.0   0.9968  3.20       0.68   
2                 15.0                  54.0   0.9970  3.26       0.65   
3                 17.0                  60.0   0.9980  3.16       0.58   
4                 11.0                  34.0   0.9978  3.51       0.56   

   alcohol  quality  
0      9.4        5  
1      9.8        5  
2      9.8        5  
3      9.8        6  
4      9.4        5  

使用str.replace时为什么“TypeError:replace()至少接受2个参数(给定1个)”有什么线索吗

顺便说一下,我用列表理解法解决了这个问题

In [6]: df.columns=[col.replace(' ', '_') for col in df.columns]
   ...: df.head()
Out[6]: 
   fixed_acidity  volatile_acidity  citric_acid  residual_sugar  chlorides  \
0            7.4              0.70         0.00             1.9      0.076   
1            7.8              0.88         0.00             2.6      0.098   
2            7.8              0.76         0.04             2.3      0.092   
3           11.2              0.28         0.56             1.9      0.075   
4            7.4              0.70         0.00             1.9      0.076   

   free_sulfur_dioxide  total_sulfur_dioxide  density    ph  sulphates  \
0                 11.0                  34.0   0.9978  3.51       0.56   
1                 25.0                  67.0   0.9968  3.20       0.68   
2                 15.0                  54.0   0.9970  3.26       0.65   
3                 17.0                  60.0   0.9980  3.16       0.58   
4                 11.0                  34.0   0.9978  3.51       0.56   

   alcohol  quality  
0      9.4        5  
1      9.8        5  
2      9.8        5  
3      9.8        6  
4      9.4        5  

但我仍然不明白为什么在pd.rename中使用replace方法时会返回TypeError消息


Tags: columnsintruedfoutheadreplacerename