Draymonders / Code-Life

The marathon continues though.
27 stars 3 forks source link

pandas #15

Open Draymonders opened 4 years ago

Draymonders commented 4 years ago

pandas入门

Draymonders commented 4 years ago

创建

自行创建

import pandas as pd
df = pd.DataFrame([[1,2,3],[4,5,6]], columns=["a", "b", "c"])

读取文件

df = pd.read_csv("SalesJan2009.csv")
Draymonders commented 4 years ago

取值

iloc读取

iloc = integer-location 是以行号列号名字来的 同numpy的索引,只不过需要加上iloc属性df.iloc[1,:]

结果
Transaction_date                   2001/2/9 4:53
Product                                 Product1
Price                                       1200
Payment_Type                                Visa
Name                                      Betina
City                Parkville                   
State                                         MO
Country                            United States
Account_Created                    2001/2/9 4:42
Last_Login                         2001/2/9 7:49
Latitude                                  39.195
Longitude                               -94.6819
Name: 1, dtype: object

loc

loc则是以kv方式来存的

df.loc[1,['Price', 'Product']]

结果
Price          1200
Product    Product1
Name: 1, dtype: object

也可通过访问对象属性来获取列值

df["Price"] or df.Price

Draymonders commented 4 years ago

赋值

大多数操作都同numpy一样

新增一列

df["Price2"] = df.Price

新增一行

df.append({"Price": 1})

然后查看df 会发现新增了一行,除了Price列为1,其余都是NaN

两个csv进行拼接

df3 = df1.append(df2, ignore_index=True)

Draymonders commented 4 years ago

复杂查询

df.Property 有str方法调用