(1)读入 CSV 数据集
pd.DataFrame.from_csv(“csv_file”)
或者:
pd.read_csv(“csv_file”)
(2)读入 Excel 数据集
pd.read_excel("excel_file")
(3)直接把数据写入 CSV
如数据以逗号分隔,且没有索引:
df.to_csv("data.csv", sep=",", index=False)
(4)基础数据集特征信息
df.info()
(5)基础数据集统计结果
print(df.describe())
(6)以表格形式打印数据
print(tabulate(print_table, headers=headers))
其中“print_table”是一列list,“headers”是一列字符串抬头
(7)列出列名称
df.columns
基本数据处理
(8)删除缺失的数据
df.dropna(axis=0, how='any')
返回给定轴上标签的对象,逐个丢掉相应数据。
(9)替换丢失的数据
df.replace(to_replace=None, value=None)
用“value”的值替换“to_replace”中给出的值。
(10)检查 NAN
pd.isnull(object)
检测缺失值(有数值数组中的NaN,对象数组中的None和NaN)
(11)删除特征
df.drop('feature_variable_name', axis=1)
轴为 0 代表行,1 代表列
(12)将对象类型转换为 float
pd.to_numeric(df["feature_name"], errors='coerce')
将对象类型转换为数字型以便计算(如果它们是字符串的话)
(13)将数据转换为 Numpy 数组
df.as_matrix()
(14)获取数据的头“n”行
df.head(n)
(15)按特征名称获取数据
df.loc[feature_name]
(16)将函数应用于数据
这个函数将数据里“height”一列中的所有值乘以2
df["height"].apply(*lambda* height: 2 * height)
或:
df["height"].apply(multiply)
(17)重命名数据列
这里我们将数据的第3列重命名为“size”
df.rename(columns = {df.columns[2]:'size'}, inplace=True)
(18)单独提取某一列
df["name"].unique()
(19)访问子数据
我们从数据中选择“name”和“size”两列
new_df = df[["name", "size"]]
(20)总结数据信息
#中位数值df.median()
(21)对数据进行排序
df.sort_values(ascending = False)
(22)布尔索引
这里我们过滤“size”的数据列,以显示等于5的值:
df[df["size"] == 5]
(23)选择某值
选择“size”列的第一行:
df.loc([0], ['size'])
<pre style="letter-spacing: 0.544px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;widows: 1;line-height: 1.75em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong>完<strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.5px;box-sizing: border-box !important;overflow-wrap: break-word !important;">—</span></strong></span></strong></span></strong></p><section style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section><p style="margin-bottom: 15px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></p><section style="margin: 5px 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">知乎高赞:985计算机视觉毕业后找不到工作怎么办?怒刷leetcode,还是另寻他路?</span></section><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">GitHub重大更新:在线开发上线,是时候卸载IDE了</p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">李沐团队半年离开六人,MxNet是否英雄落幕?</p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;box-sizing: border-box !important;overflow-wrap: break-word !important;">程序猿惯用口头禅,你中了几条?</p></section></section></section></section></section></section></section></section>
本篇文章来源于: 深度学习这件小事
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
内容反馈