[python_2: pandas]

Use of pandas for dataframe

  1. first : import the package to be used:
    import pandas as pd
    import numpy as np

  2. read csv file into python environment

data1 = pd.read_csv("data1.csv")
  1. overview the structure and characters of the dataframe.
data1.head() #default 5 rows, another wanted number is avaiable.
data1.tail(4) #display the last 4 rows in the dataframe.
data1.columns() #list the names of columns in the dataframe.
data1.shape() #similar with numpy, get the amount of columns and rows in a form of (rows, columns)
  1. index in a pandas dataframe.
data1.loc[0] # specify the first observation, the number must be less than the max row number.

data1.loc["var1"] #index a column in pandas dataframe.
# -------select specific columns---------
col_names = data1.columns.tolist()
wanted_columns = []
for c in col_names:
	if c.endswith("(g)"):
		wanted_columns.append(c)
wanted_df = data1[wanted_columns]
print(wanted_df.head(3))
#-----------------------------------------------
  1. insert a variable into a pandas dataframe(the number of rows must be consistent)
data1["var_name"] = var
  1. index the max or min value in a column
data1["column_name"].max()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值