Pandas笔记
读取数据👉read_xxx
读取excel的数据
df = pd.read_excel(item_file_path) |
读取csv的数据
df = pd.read_csv(file_path, encoding='gbk') |
遍历数据👉iterrows、iteritems
行遍历👉iterrows
for index, obj in item_df.iterrows(): |
列遍历👉iteritems
for col_name, col in item_df.iteritems(): |
过滤列数据找到对应行数据👉[[condition]]
过滤列名为col_name的数据等于val值的行数据,将过滤得到的数据赋值到df_data变量上。
注:需要将获取得到的对应列的值(df['col_name'])和对比的值(val)转为字符型,
df_data = df[df['col_name'].astype(str) == str('val')] |
列转为List👉tolist
将该列col_name的所有数据转为List结构
df['col_name'].tolist() |
修改行对应的列数据👉loc[i, c]
将第index行的数据,对应的列col_name,修改值为new_val
df.loc[index, 'col_name'] = 'new_val' |
添加列👉[‘new_col_name’]
往表格中添加新列new_col,并且赋值为new_col_val
注:需要保存当前的df才能在文件中看到效果
df['new_col'] = 'new_col_val' |
保存文件👉to_xxx
将当前修改过的df保存到output_file.xls文件中。
注:文件名需要加上后缀
df.to_excel('output_file.xls', index=False) |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 玩转代码:探索奇妙之地!




