Import NumPy as NP import pandas as PD from pandas import series,dataframe ' If copied code, error syntaxerror:invalid character
In identifier, there is a space for the Chinese symbol in the copied code. "DATA=PD." Dataframe (Np.arange (6). Reshape ((3,2)), INDEX=PD. Index ([' A ', ' B ', ' C '],name= ' state '), COLUMNS=PD. Index ([' I ', ' II '],name= ')] Print (data) ' number I II state a 0 1 B 2 3 C 4 5
Time resampling of Pandas data Visualization (iii)
Python+pandas generate the specified date and resampling-CSDN blog https://blog.csdn.net/LY_ysys629/article/details/73823803
Pandas Resample Method-Csdn Blog https://blog.csdn.net/wangshuang1631/article/details/52314944
——————————————————————————————————————————————————
Time Series Conversions:
C=PD. Seri
', DF ['v1']) #2 indicates the insert position, and V6 indicates the column name, DF ['v1 '] is the inserted value print ('insert column:') print (DF, '\ n') print (' * 50)
4. General selection methods:
Operation Method
Method
Result
Select a column
Def [col]
Sequence
Select a row using column tags
DF. Loc [col]
Sequence
Select a row by location
DF. icol [2]
Sequence
Line Cutting
DF [5: 10]
Data box
Dateframe modifying column names in pandasWhen doing data mining, want to change a dataframe column name, so looked up, summarized as follows:The data are as follows:>>>Import PandasAs pd>>>a = PD. DataFrame ({' A ': [1,2,3], ' B ': [4,5,6], ' C ': [7,8,9]})> >> a a B C0 1 4 71 2 5 82 3 6 9 /c21> Method One: Methods of violence>>>a.columns = [‘a‘,‘b‘,‘c‘]>>>a a b c0 1 4 71 2 5 82 3 6 9But the disadvantage is to write three, or error.Method Two: A better method>>>a.rename(columns={‘A‘:‘a‘, ‘
Workaround:Pd_data = pd.read_table (comment_file,header=none,encoding='utf-8', engine=' python ')Official website Analysis:engine : {' C ', ' Python '}, optional
Parser engine to use. The C engine was faster while the Python engine was currently more feature-complete.
1,
iterator : boolean, default False
Return Textfilereader object for iteration or getting chunks Withget_chunk () .
or get
from Chunk
pd_data = pd.read_table (comme
A few tips that you think are more useful.DF is a dataframeSE is a series1, import data, often need to see what the data look like, this time need. Head (n) function,That is, the first n rows of data are displayed.Df.head (5)Se.head (5)2, want to know how many columns df, what is the specific content of the column, with Df.columns3. If you want to know how many different elements are in a column or SE of DF, use the. value_counts () functiondf[' mm '].value_counts ()Se.value_counts ()
installation of PandasCMD window inputPip Install PandasV. Testing1, now the Python interactive mode and under the Pycharm editor are not error.,2, PIP installation JupyterPip Install Jupyter3. cmd command to open Notebook#cmd命令jupyter Notebook4. Open a Jupyter notebook Click File New to select Python version 2 Enter the following code click the cell run all to execute the code#coding: Utf-8import Matplotlib.pyplot as Pltimport numpy as NpX = Np.linspace (-np.pi,np.pi,256,endpoint=true) (C,S) =
data conversion refers to filtering, cleaning, and other conversion operations on the data. Remove Duplicate data Repeating rows often appear in the Dataframe, Dataframe provides a duplicated () method to detect whether rows are duplicated, and another drop_duplicates () method to discard duplicate rows:Duplicated () and Drop_duplicates () methods defaultJudging all Columns, if you do not want to, the collection of incoming columns as a parameter can be specified as a column, for example:Dupl
minus minimum for each columnThe Apply function enters a sequenceValue_counts () View the number of occurrences of an element with. Mode () View the most frequently occurring elementsCreate a random sequence firstCall Value_counts ()Call. Mode () to see the most frequently occurring elementsData mergeCreate an array of 10*4 first(1) Call the concat () function to merge the array (Concat accepts an array, which is the array to be merged)See if the merged array is equal to the original arrayOr(2)
Explore the students ' consumption of wineData See GitHubStep 1-Import the necessary librariesImport Pandas as PD Import NumPy as NPStep 2-Data set" ./data/student-mat.csv " Step 3 Name The data studentStudent = Pd.read_csv (PATH4) Student.head ()Output:Step 4 Slice the data from ' school ' to ' Guardian '" School ":"Guardian"]stud_alcoh.head ()Output:Step 5 Create a lambda function that captures a stringLambda x:x.upper ()Step 6 capitalize the ' Fjo
The most by a friend set up a part-time operation of the company, but the need for some part-time staff pay, but due to a part-time wage between the 40~60, so the company adopted the principle is more than 200 to carry out, this rule is equivalent to drop the driver, the withdrawal needs more than 200, Then the problem came, in order to better let a large number of part-time staff can, clearly understand the time period in which they earn a lot of money, this time extended a problem, we need to
the unique value of A, the number of occurrences (a, b) of the unique value of statistics = (1,3) c appears 1 times (A, B) = (2,4) appears 3 times - the Print(Pd.crosstab (df['A'],df['B'],normalize=true))#display in a frequency-based manner - Print('--------') - Print(Pd.crosstab (df['A'],df['B'],values=df['C'],aggfunc=np.sum))#values: A value array based on a factor aggregation - #Aggfunc: If the values array is not passed, the frequency table is computed, and if the array is passed, the calc
Ming 6.0 - Name:price, Dtype:float64 -Zhang San 1.2 theReese 1.0 -Harry 2.3 -Chen Jiu 5.0 -Xiao Ming 6.0 +Name:price, Dtype:float64 In general, we often need to value by column, then Dataframe provides loc and Iloc for everyone to choose from, but the difference is between the two.1 Print(frame2)2 Print(frame2.loc['Harry'])#Loc can use the index of the string type, whereas the Iloc can only be of type int3 Print(frame0.iloc[2])4 out[2]: 5 Color Object Price6Zhang San Blue ball 1.27Reese Green
1. Create a dataframe from a dictionary>>>ImportPandas as PD>>> Dict1 = {'col1': [1,2,5,7],'col2':['a','b','C','D']}>>> DF =PD. DataFrame (Dict1)>>>DF col1 COL201a1 2b2 5C3 7 D2. Create Dataframe from multiple lists (convert the list to a dictionary, then convert the dictionary to dataframe)>>> lista = [1,2,5,7]>>> LISTB = ['a','b','C','D']>>> df = PD. DataFrame ({'col1': Lista,'col2': Listb})>>>DF col1 COL201a1 2b2 5C3 7 DPython Pandas Dataframe oper
Sometimes we can rank and sort series and dataframe based on the size of the index or the size of the value. A, sorting
Pandas provides a Sort_index method that sorts A, series sort 1, sorted by index based on the index of rows or columns in the order of the dictionary.
#定义一个Series
s = Series ([1,2,3],index=["A", "C", "B"])
#对Series的索引进行排序, the default is ascending
print (S.sort_index ())
'
a 1
b 3
C 2
'
Official documents:
Pandas. Dataframe.unstack¶Dataframe. Unstack (Level=-1, fill_value=none) [source]¶
Pivot A level of the (necessarily hierarchical) index labels, returning a DATAFRAME has a new level of column labels WH OSE Inner-most level consists of the pivoted index labels. If The index is not a multiindex, the output would be a Series (the analogue's stack when the columns are not a multiindex (when there is only one row index, the result gene
date belongs to a leap year
Import pandas as PD
Df=pd.read_excel ("C:/users/administrator/desktop/new Microsoft Excel worksheet. xlsx") #读取工作表
DF [Property],df[' Description ']=df[' property Description '].str.split ("", n=1). str# divide by first space
Df.drop ("Property Description ", axis=1,inplace=true) #删除原有的列
df.to_csv (" C:/users/administrator/desktop/new Microsoft Excel Worksheet. csv ", Index=false) #保存为csv, and delete the index
Th
Statistical methods
Pandas objects have some statistical methods. Most of them are reduction and summary statistics that are used to extract a single value from a Series, or to extract a Series from a dataframe row or column.
For example, the Dataframe.mean (axis=0,skipna=true) method, when NA values are present in the dataset, are simply skipped, unless the entire slice (row or column) is all NA, and if you do not want to, you can disable this feat
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.