50 个常见的 Python 数据分析小方法(上)
Python中文社区
共 2191字,需浏览 5分钟
·
2020-12-04 20:53
df.info() #查看数据类型
df.shape #查看数据规模
df.describe() #数据统计信息描述
Q2:如何设置才能不隐藏DataFram的列?
pd.set_option("max_columns",100) #这里100可以调整:最大显示列数
pd.set_option('display.max_columns',None) #这种是都显示
df.isnull().sum()
df.duplicated().any()
df.fillna(method = "ffill") #这是前向方法填充,bfill为后向填充
df.fillna(0) #用0填充空值
df.drop([""], axis =1, inplace = True)
df.dropna(axis = 0, how = 'any', inplace = True)
df.drop_duplicates(inplace=True)
df = df[~df["列名"].isin(['这里写特殊值/错误值'])]
df[""] = df[""].astype(int)
df = df.reset_index(drop = True)
m = df[''].sort_values(ascending = False).index[:].tolist()
df = df.loc[m]
df = df.reset_index(drop = True)
s = df.groupby("列")["指标列"].agg(["count","sum","mean"])
s = s[s["count"]>20]
s.sort_values("mean", ascending = False).head(10)
df = df.groupby('',as_index = False).count[['','']]
df.rename(columns = {'原来的列名':'新的列名'},inplace = True)
data['时间']=pd.to_datetime(data['时间'])
df['age'] = (pd.to_datetime('这里是当前日期如:2020-4') - pd.to_datetime(df['birthday'])) / pd.Timedelta('365 days')
t = df[""].str.split("\",expand = True)
t[0]
cut_bins = np.arrange(90,130,5)#分段设置,这里是分成5段
bins = pd.cut(df['score'], cut_bins)#将数据切片
bin_counts = df['score'].groupby(bins).count()
df[''].value_counts()
df[''].value_counts().plot(kind = "bar")
df.corr()
df.plot.scatter(x="",y = "", figsize=(,), title = "")
fig = df[['','']].plot(kind = "kde", figsize = (24,8), title = "")
fig.axes.title.set_size(10)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
X = df.drop(['y'],axis = 1)
y = df['y']
model.fit(X, y)
y_pre = model.predict(test)
推荐阅读
点击下方阅读原文加入社区会员
点赞鼓励一下
评论