程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning: precautions in pandas

編輯:Python

df[“column”] and df.column The difference between

import numpy as np
import pandas as pd
columns = ["name","id"]
names = ["wangfang","zhangsan","jeff","peter","hell"]
ids = [i** 2 for i in range(5)]
df = pd.DataFrame(data=np.array([names,ids]).T,columns=columns)
df
nameid0wangfang01zhangsan12jeff43peter94hell16
  • adopt df.column and df[“column”] The column values can be obtained in any of the following ways
  • however df.column There are restrictions on the way : That's it column The name of must be valid

Valid column name presentation

df.name
0 wangfang
1 zhangsan
2 jeff
3 peter
4 hell
Name: name, dtype: object
df["name"]
0 wangfang
1 zhangsan
2 jeff
3 peter
4 hell
Name: name, dtype: object

Invalid column name Demo

series_ = pd.Series([24,38,66,52,31])
df[" Age ~ yes "]= series_
df
nameid Age ~ yes 0wangfang0241zhangsan1382jeff4663peter9524hell1631
# You can't pass at this time df.column To get the data of this column 
df[" Age ~ yes "]
0 24
1 38
2 66
3 52
4 31
Name: Age ~ yes , dtype: int64

df.column Columns cannot be created or modified df

# In this way, you can create , You can also batch modify column values 
df["add"] = [1,2,3,4,5]
df
nameid Age ~ yes add0wangfang02411zhangsan13822jeff46633peter95244hell16315
df["add"][0]= 100
df
D:\Anaconda3\envs\data\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
"""Entry point for launching an IPython kernel.
nameid Age ~ yes add0wangfang0241001zhangsan13822jeff46633peter95244hell16315
# What you get with attributes is a copy , Can not be used against the original df Make changes , You cannot create new columns 
# Cannot be modified directly df
df.add = [1,6,4,2,2]
df.add[0] = 10
# Cannot create new column 
df.add_ = [0,0,0,0,0]
df
nameid Age ~ yes add0wangfang0241001zhangsan13822jeff46633peter95244hell16315

index Repeatable but not modifiable

index It's repeatable

data = np.array([1,2,4,5,8])
series = pd.Series(data=data,index=['a','a','b','c','c'])
series
a 1
a 2
b 4
c 5
c 8
dtype: int32
series["a"]
a 1
a 2
dtype: int32

index It cannot be modified separately

series.index[0] = "4"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-a68b0d9a21b2> in <module>
----> 1 series.index[0] = "4"
D:\Anaconda3\envs\data\lib\site-packages\pandas\core\indexes\base.py in __setitem__(self, key, value)
4082
4083 def __setitem__(self, key, value):
-> 4084 raise TypeError("Index does not support mutable operations")
4085
4086 def __getitem__(self, key):
TypeError: Index does not support mutable operations

index It can be replaced in batches

  • But be sure to give index The length of the array is consistent with the original data
  • adopt reindex Method does not need to keep the data length consistent ; Because items that do not exist will follow NAN Value filling

series.index = [str(i) for i in range(5)]
series
0 1
1 2
2 4
3 5
4 8
dtype: int32
series.reindex([str(i) for i in range(6)])
0 1.0
1 2.0
2 4.0
3 5.0
4 8.0
5 NaN
dtype: float64
  • Of course NaN Value can be used method The method in , choice ffill Is to fill in with the previous value
series.reindex([str(i) for i in range(6)],method="ffill")
0 1
1 2
2 4
3 5
4 8
5 8
dtype: int32

reindex Common parameters of

  • method: The way of interpolation “ffill” “bfill” Fill in the preceding item and fill in the following item
  • fill_value: Fill with a special value
  • limit: The maximum dimension gap to be filled when filling the front or rear items
  • tolerance: When the preceding or subsequent items are filled , The maximum dimension gap under the inaccurate match to be filled
  • level
  • copy


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved