df1=pd.read_excel("ren.xlsx") df1["工资"].replace("","其他",inplace=True) df1["公司性质"].replace({"不限":"其他","":"其他"},inplace=True) print (df1)
时间: 2023-11-30 19:02:15 浏览: 31
这段代码是用来读取名为"ren.xlsx"的Excel文件,并对其中的"工资"和"公司性质"两列进行数据清洗。具体来说,将"工资"列中的空值替换为"其他",将"公司性质"列中的"不限"和空值替换为"其他"。最后输出清洗后的数据框。
```python
import pandas as pd
df1 = pd.read_excel("ren.xlsx")
df1["工资"].replace("", "其他", inplace=True)
df1["公司性质"].replace({"不限": "其他", "": "其他"}, inplace=True)
print(df1)
```
相关问题
#%% import pandas as pd import numpy as np import matplotlib.pyplot as plt # 读取Excel文件中的所有表格 excel_file = pd.ExcelFile('附件1-装箱数据.xlsx') # 读取指定表格 df1 = pd.read_excel(excel_file, sheet_name='订单数据') df2 = pd.read_excel(excel_file, sheet_name='耗材数据') # 检查是否有缺失值 print(df1.isnull().sum()) print(df2.isnull().sum()) print(df1.head()) print(df2.head()) df2=df2['耗材类型'].replace({'袋':1, '箱': 0}, inplace=True) df2.drop(columns=['重量']).values print(df1.head()) print(df2.head())
这段代码主要是对Excel文件中的两个表格进行读取,并进行了一些简单的数据处理和检查缺失值的操作。具体解释如下:
- 首先,通过 pandas 库中的 ExcelFile 类读取 Excel 文件中的所有表格。
- 然后,使用 read_excel() 方法读取指定的表格,并将其存储为 DataFrame 对象,分别命名为 df1 和 df2。
- 接着,使用 isnull() 和 sum() 方法检查 df1 和 df2 中是否有缺失值。
- 然后,使用 head() 方法查看 df1 和 df2 的前几行数据,以确保读取正确。
- 接下来,对 df2 中的耗材类型进行转换,转换为数字表示,袋为1,箱为0。
- 然后,使用 drop() 方法删除 df2 中的重量列,并使用 values 属性将其转换为 NumPy 数组。
- 最后,再次使用 head() 方法查看 df1 和 df2 的前几行数据,以确保数据处理和转换正确。
需要注意的是,代码中的 df2=df2['耗材类型'].replace({'袋':1, '箱': 0}, inplace=True) 这一行不是很合理,因为它实际上没有对 df2 进行任何操作。正确的写法应该是 df2['耗材类型'].replace({'袋':1, '箱': 0}, inplace=True)。
import pandas as pd import datetime df_201=pd.read_excel(r'C:\Users\82422\Desktop\问E讲堂excel\5.04问医晚上数据.xlsx',sheet_name='邀请用户信息') df_201=df_201.drop(['编号','用户ID'],axis=1) #df_201=df_201.rename(columns={'累计观看时长': '2023.5.04累计观看时长'}) #df_201=df_201.rename(columns={'累计观看时长': str(datetime.date.today())+'累计观看时长'}) #将累计观看时长更改成当前日期的累计观看时长 print(df_201.head())import pandas as pd import os df1=pd.DataFrame() for root,dirs,files in os.walk(r'C:\Users\82422\Desktop\问E讲堂excel'): print('root:\n',root) print('dirs:\n',dirs) print('files:\n',files) for file in files: file_name=os.path.join(root,file) df=pd.read_excel(file_name,sheet_name='邀请用户信息') df=df.drop(['编号','用户ID'],axis=1) df=df.rename(columns={'累计观看时长': file[0:4] +'累计观看时长'}) df_201=pd.merge(df,df_201,on=['邀请人昵称','用户昵称'],how='outer') print(df_201) df_201.to_excel(r'C:\Users\82422\Desktop\问E讲堂excel\h2.xlsx',sheet_name='邀请用户信息')优化这个代码
import pandas as pd
import os
# 读取 5.04问医晚上数据.xlsx 中的邀请用户信息,并删除编号和用户ID列
df_201 = pd.read_excel(r'C:\Users\82422\Desktop\问E讲堂excel\5.04问医晚上数据.xlsx', sheet_name='邀请用户信息')
df_201.drop(['编号', '用户ID'], axis=1, inplace=True)
# 将累计观看时长更改成当前日期的累计观看时长
today = pd.Timestamp.today().strftime('%Y.%m.%d')
df_201.rename(columns={'累计观看时长': today + '累计观看时长'}, inplace=True)
# 遍历问E讲堂excel目录下的所有.xlsx文件,读取邀请用户信息,并合并到df_201中
for root, dirs, files in os.walk(r'C:\Users\82422\Desktop\问E讲堂excel'):
for file in files:
if file.endswith('.xlsx'):
file_name = os.path.join(root, file)
if file_name != r'C:\Users\82422\Desktop\问E讲堂excel\5.04问医晚上数据.xlsx':
df = pd.read_excel(file_name, sheet_name='邀请用户信息')
df.drop(['编号', '用户ID'], axis=1, inplace=True)
date = file.split('.')[0] # 获取文件名中的日期
df.rename(columns={'累计观看时长': date + '累计观看时长'}, inplace=True)
df_201 = pd.merge(df, df_201, on=['邀请人昵称', '用户昵称'], how='outer')
# 将合并后的结果保存为 h2.xlsx
df_201.to_excel(r'C:\Users\82422\Desktop\问E讲堂excel\h2.xlsx', sheet_name='邀请用户信息', index=False)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)