[python3][DataFrame保存为excel前需要做的几个步骤]

零、创建DataFrame

import pandas as pd
df = pd.DataFrame([{'winner':'jsr'},
                   {'id':0,'winner':'jsr','age':'19940121','take_on':'vocal'},
                   {'id':1,'winner':'jqy','age':'19910926','take_on':'vocal'},
                   {'id':2,'winner':'smh','age':'19930330','take_on':'rapper'},
                   {'id':3,'winner':'lsx','age':'19920111','take_on':'rapper'},
                   {'id':7,'winner':'lsx','take_on':'rap'},
                   {'id':3,'winner':'lsx','age':'44444444'},
                   {'winner':'lsx','age':'19920111','take_on':'rap'},
                   {'id':1,'winner':'jqy','age':'19910926','take_on':'vocal'},
])
print(df)## 得到原始的DataFrame数据

在这里插入图片描述

一、填补空值

fi_values = {'id': 4444, 'age': 44}
df.fillna(value=fi_values,inplace=True)
print(df)## type(df.loc[6,'age'])#为int
## 在id这一列,用4444替代为空的元素;在age这一列,用44替代为空的元素
df.fillna(method='ffill',inplace=True)
## method = 'bfill'## 用后一个非缺失值填补缺失值
print(df)## 用该列紧邻的上方的元素替代下方为空的元素
df.fillna('',inplace=True)
print(df)## 用''替代为空的元素

在这里插入图片描述
在这里插入图片描述

二、去重(包括所有列,个别列)

## 默认全部列都参与;若出现重复的行,默认保留第一个出现的行;
df.drop_duplicates(inplace = True)
print(df)## 查看全部行是否存在重复的行,删除重复的行

df.drop_duplicates(['winner','take_on'],inplace = True,keep = "first")
print(df)## 选定'winner''age'这两列;若出现重复的行,保留第一个出现的行;
## 注意筛选的是一个列表,无论列表中是否只有一个元素

df.drop_duplicates(['winner'],inplace = True,keep = "last")
print(df)## 选定'winner''age'这两列;若出现重复的行,保留最后一个出现的行;

在这里插入图片描述

三、重新设置索引

df.reset_index(inplace = True,drop = True)
print(df)## 行索引的重新排序

df.reset_index(inplace = True)
print(df)## 行索引的重新排序,并另取一列表示索引#df['index']

在这里插入图片描述

四、选择自己需要的列

df = df[['index','winner','age']]
print(df)## 行索引的重新排序,并另取一列表示索引#df['index']

#del df['age'],df['id']## 删除不要的列

在这里插入图片描述

五、重命名DataFrame的列名称

df.columns = ['id','name','age']
print(df)## 暴力修改法

df.rename(columns = {'id':'re_id'}, inplace=True)
print(df)## 温和修改法

在这里插入图片描述

六、保存为excel文件

df.to_excel('winner.xlsx',index = False)
#df.to_csv('winner.csv',index = False)
  • 11
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中横向合并DataFrame,可以使用pandas库的concat方法。具体操作步骤如下: 1. 首先导入必要的库: ```python import pandas as pd ``` 2. 读取要合并的DataFrame数据: ```python df1 = pd.read_excel('文件路径1.xlsx', sheet_name='工作表1') df2 = pd.read_excel('文件路径2.xlsx', sheet_name='工作表2') ``` 3. 使用concat方法进行横向合并: ```python df_combined = pd.concat([df1, df2], axis=1) ``` 其中,参数axis=1表示横向合并。 4. 打印合并后的结果: ```python print(df_combined) ``` 以上是使用concat方法进行DataFrame横向合并的步骤和代码示例。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Pandas统计分析基础(5):DataFrame的合并(内含大量代码可供练习)](https://blog.csdn.net/l1796333514/article/details/123651411)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python——把几个DataFrame合并成一个DataFrame](https://blog.csdn.net/J__aries/article/details/128924927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值