新建dataframe
import pandas as pd
a = pd.DataFrame([[1,2,3],
[4,5,6],
[7,8,9]],columns = ["feature_1", "feature_2", "label"])
读取
import pandas as pd
df = pd.read_csv("datas/hour.csv", sep=",")
删除dataframe列
del df["instant"]
df.drop(columns=["instant","dteday"])
修改dataframe列名
暴力
a.columns = ['a','b','c']
较好的方法
a.rename(columns={'A':'a', 'B':'b', 'C':'c'}, inplace = True)
查看dataframe字段信息
a.info()
修改dataframe列类型
df["instant"] = df["instant"].astype("object")
X[['Global_active_power',"b"]] = X[['Global_active_power',"b"]].astype('float64')
查看dataframe统计信息
a.describe()
获取dataframe部分列
a.iloc[:,0:3]
df.iloc[:,[-1]]
a[["feature_1", "feature_2"]]
获取dataframe列名
df.columns返回一个可迭代对象
for i in df.columns:
print(i)
获取dataframe的Series
一行
a.iloc[0,:]
一列
a.iloc[:,1]
a["feature_1"]
合并dataframe
横向
pd.concat([a,a],axis=1)
纵向
pd.concat([a,a],axis=0)
替换DF中的字符串
#df.int_rate.replace('%','',inplace = True, regex = True)
a.replace('%','',inplace = True, regex = True)
Dataframe copy
import pandas as pd
a = pd.DataFrame([[1,2,3],
[4,5,6],
[7,8,9]],columns = ["feature_1", "feature_2", "label"])
b = a.copy()
b.drop(columns=["feature_1"],inplace=True)
a