190414-Manipulating DataFrames with pandas-Extracting and transforming data

1.Indexing DataFrame

  • Indexing using square brackets

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第1张图片

  • Using column attribute and row labels

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第2张图片

  • Using the .loc/.iloc accessor

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第3张图片190414-Manipulating DataFrames with pandas-Extracting and transforming data_第4张图片

  • selecting only some columns

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第5张图片

2. Slicing DataFrame

  • 数据中某一列的数据类型是Series, Series 是有index的,介于np arrays和dict之间
  • Slicing not using accessor

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第6张图片

  • Slicing using .loc/.iloc accessor

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第7张图片

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第8张图片

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第9张图片190414-Manipulating DataFrames with pandas-Extracting and transforming data_第10张图片

  • using list rather than slices

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第11张图片

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第12张图片

  • series versus 1-column dataframe

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第13张图片

  • Slicing rows

.loc['b':'a':-1] -1可以表示倒序;另外.loc里,此处列值部分放空也可以

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第14张图片

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第15张图片

3. Filtering DataFrames

  • Filtering with a Boolean Series

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第16张图片

  • Combining filters

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第17张图片

  • selecting columns with all nonzeros

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第18张图片

  • selecting columns with any nonzeros

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第19张图片

  • Selecting columns with any NaNs

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第20张图片

  • Selecting columns without NaNs

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第21张图片

  • Drop rows with any NaNs

如果换成all了name就是当该行全部都是NaN才会被drop

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第22张图片

  • Filtering a column based on another

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第23张图片

  • Modifying a column based on another

salt 大于55的,egg值加5

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第24张图片

  • Filtering using NaNs:

thresh=1000表示less than 1000, axis=‘columns' 是因为axis默认是0,指的是rows

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第25张图片

3. Transforming DataFrames

  • DataFrame vectorized methods:

df.floordiv()

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第26张图片

  • numpy vectorized functions:

np.floor.divide(df,12)

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第27张图片

  • Plain python function(1)

def

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第28张图片

plain python function(2)

  • lambda function

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第29张图片

  • storing transformation

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第30张图片

  • working with string values

str.upper(): 小写转大写

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第31张图片

  • index没有apply method,用.map();

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第32张图片

  • 用两列的运算定义新的列:

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第33张图片

  • Using apply() to transform a column

用完def用apply,但好像不用apply也能做的。。。

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第34张图片

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第35张图片

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第36张图片

  • Using .map() with a dictionary

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第37张图片

  • Using vectorized functions

注意导入的方式

190414-Manipulating DataFrames with pandas-Extracting and transforming data_第38张图片

你可能感兴趣的:(Python,for,Data,Science)