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
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
series_ = pd.Series([24,38,66,52,31])
df[" Age ~ yes "]= series_
df
# 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
# In this way, you can create , You can also batch modify column values
df["add"] = [1,2,3,4,5]
df
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.
# 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
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
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
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
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