This article introduces the simple operation methods of pandas.DataFrame (creation, indexing, addition and deletion) in python, including related information on creation, indexing, addition and deletion, etc. The introduction in the article is very detailed. Friends who need it can For reference, let’s take a look below.
Preface
Recently, I have searched a lot of operation instructions on the Internet for pandas.DataFrame
, all of which are basic. operations, but the combination of these operations still takes time to correctly operate the DataFrame, and it took me a long time to adjust the bug. I will make some summaries here for the convenience of you, me and others. Friends who are interested, please come and take a look.
1. Simple operation to create DataFrame:
1. Create according to dictionary:
In [1]: import pandas as pd In [3]: aa={'one':[1,2,3],'two':[2,3,4],'three':[3,4,5]} In [4]: bb=pd.DataFrame(aa) In [5]: bb Out[5]: one three two 0 1 3 2 1 2 4 3 2 3 5 4`
The keys in the dictionary are the columns in the DataFrame, but there is no index value, so you need to set it yourself. If not set, the default is to start counting from zero.
bb=pd.DataFrame(aa,index=['first','second','third']) bb Out[7]: one three two first 1 3 2 second 2 4 3 third 3 5 4
2. Create from a multi-dimensional array
import numpy as np In [9]: del aa In [10]: aa=np.array([[1,2,3],[4,5,6],[7,8,9]]) In [11]: aa Out[11]: array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) In [12]: bb=pd.DataFrame(aa) In [13]: bb Out[13]: 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9
To create from a multi-dimensional array, you need to assign columns and index to the DataFrame, otherwise it will be the default, which is ugly.
bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three']) In [15]: bb Out[15]: one two three 22 1 2 3 33 4 5 6 44 7 8 9
3. Create with other DataFrame
bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three']) bb Out[15]: one two three 22 1 2 3 33 4 5 6 44 7 8 9 cc=bb[['one','three']].copy() Cc Out[17]: one three 22 1 3 33 4 6 44 7 9
The copy here is a deep copy. Changing the value in cc cannot change the value in bb.
cc['three'][22]=5 bb Out[19]: one two three 22 1 2 3 33 4 5 6 44 7 8 9 cc Out[20]: one three 22 1 5 33 4 6 44 7 9
2. Index operation of DataFrame:
For a DataFrame, indexing is the most troublesome and error-prone.
1. Indexing one or more columns is relatively simple:
bb['one'] Out[21]: 22 1 33 4 44 7 Name: one, dtype: int32
For multiple column names, the input column names need to be stored in a list to be a collerable variable. , otherwise an error will be reported.
bb[['one','three']] Out[29]: one three 22 1 3 33 4 6 44 7 9
2. Index one record or several records:
bb[1:3] Out[27]: one two three 33 4 5 6 44 7 8 9 bb[:1] Out[28]: one two three 22 1 2 3
Note here that the colon is required, otherwise it will be an index column. .
3. Index certain records of variables in certain columns. This tortured me for a long time:
First type
bb.loc[[22,33]][['one','three']] Out[30]: one three 22 1 3 33 4 6
You cannot change the value here. You can only read the value but not write it. It may be related to the loc()
function:
bb.loc[[22,33]][['one','three']]=[[2,2],[3,6]] In [32]: bb Out[32]: one two three 22 1 2 3 33 4 5 6 44 7 8 9
The second type: also only You can see
bb[['one','three']][:2] Out[33]: one three 22 1 3 33 4 6
If you want to change the value, an error will be reported.
In [34]: bb[['one','three']][:2]=[[2,2],[2,2]] -c:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead F:\Anaconda\lib\site-packages\pandas\core\frame.py:1999: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame return self._setitem_slice(indexer, value)
The third type: you can change the value of the data! ! !
Iloc is indexed according to the number of rows and columns of data, not counting index and columns
bb.iloc[2:3,2:3] Out[36]: three 44 9 bb.iloc[1:3,1:3] Out[37]: two three 33 5 6 44 8 9 bb.iloc[0,0] Out[38]: 1
The following is the proof:
bb.iloc[0:4,0:2]=[[9,9],[9,9],[9,9]] In [45]: bb Out[45]: one two three 22 9 9 3 33 9 9 6 44 9 9 9
3. In the original Create a new column or several columns on the DataFrame
1. Use nothing. You can only create one column separately. Multiple columns are not easy to use. Personal test is invalid:
bb['new']=[2,3,4] bb Out[51]: one two three new 22 9 9 3 2 33 9 9 6 3 44 9 9 9 4 bb[['new','new2']]=[[2,3,4],[5,3,7]] KeyError: "['new' 'new2'] not in index"
The list assigned is basically assigned in the order of the given index value, but generally we need to assign the corresponding index. If you want more advanced assignments, look at the following.
2. Use a dictionary to assign values to multiple columns by index:
aa={33:[234,44,55],44:[657,77,77],22:[33,55,457]} In [58]: bb=bb.join(pd.DataFrame(aa.values(),columns=['hi','hello','ok'],index=aa.keys())) In [59]: bb Out[59]: one two three new hi hello ok 22 9 9 3 2 33 55 457 33 9 9 6 3 234 44 55 44 9 9 9 4 657 77 77
Here aa is a nested dictionary and list, which is equivalent to a record. Use keys as index name instead of the usual default column names. The purpose of matching multiple columns by index is achieved. Since the storage of dict()
is chaotic, using dict()
without assigning its index value will cause confusion in the records. This is worth noting.
4. Delete multiple columns or records:
Delete columns
bb.drop(['new','hi'],axis=1) Out[60]: one two three hello ok 22 9 9 3 55 457 33 9 9 6 44 55 44 9 9 9 77 77
Delete record
bb.drop([22,33],axis=0) Out[61]: one two three new hi hello ok 44 9 9 9 4 657 77 77
Share with you an article about summing rows and columns and adding new rows and columns in pandas.DataFrame in python. Friends who are interested can take a look.
There are many functions of DataFrame that have not been covered yet. They will be covered in the future. After reading the API on the official website, I will continue to share it. Everything is ok.
Related articles:
About pandas.DataFrame in python to sum rows and columns and add new rows and columns sample code
The above is the detailed content of Introduction to simple operation methods of pandas.DataFrame (create, index, add and delete) in python. For more information, please follow other related articles on the PHP Chinese website!