python将本地的excel表格中通过列标题指定的某一列数据,用8位二进制替代该列中的文本,当出现“学习、查资料”字符串第一位加1,出现“浏览新闻”第二位加1,出现“收发邮件”第三位加1,出现“娱乐游戏”第四位加1,出现“聊天交友”第五位加1,出现“资源下载”第六位加1,出现“上网购物”第七位加1,出现“其他”第八位加1,若这些都没有出现不进行处理,最后将这8位二进制分开成8个单元格保存在另一个excel表格中
时间: 2023-11-27 16:03:24 浏览: 60
可以使用Python中的pandas库来读取和处理Excel表格。具体实现步骤如下:
1. 导入pandas库和numpy库
```python
import pandas as pd
import numpy as np
```
2. 读取本地的Excel表格
```python
df = pd.read_excel('example.xlsx')
```
3. 获取指定列的数据
```python
data = df['指定列标题']
```
4. 根据字符串出现情况对数据进行处理
```python
result = []
for item in data:
binary = '00000000'
if '学习' in item or '查资料' in item:
binary = binary[:0] + '1' + binary[1:]
if '浏览新闻' in item:
binary = binary[:1] + '1' + binary[2:]
if '收发邮件' in item:
binary = binary[:2] + '1' + binary[3:]
if '娱乐游戏' in item:
binary = binary[:3] + '1' + binary[4:]
if '聊天交友' in item:
binary = binary[:4] + '1' + binary[5:]
if '资源下载' in item:
binary = binary[:5] + '1' + binary[6:]
if '上网购物' in item:
binary = binary[:6] + '1' + binary[7:]
if '其他' in item:
binary = binary[:7] + '1'
result.append(binary)
```
5. 将处理结果拆分成8个单元格并添加到原表格中
```python
df['二进制1'] = [i[0] for i in result]
df['二进制2'] = [i[1] for i in result]
df['二进制3'] = [i[2] for i in result]
df['二进制4'] = [i[3] for i in result]
df['二进制5'] = [i[4] for i in result]
df['二进制6'] = [i[5] for i in result]
df['二进制7'] = [i[6] for i in result]
df['二进制8'] = [i[7] for i in result]
```
6. 将处理后的表格保存到另一个Excel表格中
```python
df.to_excel('result.xlsx', index=False)
```
完整代码如下:
```python
import pandas as pd
import numpy as np
df = pd.read_excel('example.xlsx')
data = df['指定列标题']
result = []
for item in data:
binary = '00000000'
if '学习' in item or '查资料' in item:
binary = binary[:0] + '1' + binary[1:]
if '浏览新闻' in item:
binary = binary[:1] + '1' + binary[2:]
if '收发邮件' in item:
binary = binary[:2] + '1' + binary[3:]
if '娱乐游戏' in item:
binary = binary[:3] + '1' + binary[4:]
if '聊天交友' in item:
binary = binary[:4] + '1' + binary[5:]
if '资源下载' in item:
binary = binary[:5] + '1' + binary[6:]
if '上网购物' in item:
binary = binary[:6] + '1' + binary[7:]
if '其他' in item:
binary = binary[:7] + '1'
result.append(binary)
df['二进制1'] = [i[0] for i in result]
df['二进制2'] = [i[1] for i in result]
df['二进制3'] = [i[2] for i in result]
df['二进制4'] = [i[3] for i in result]
df['二进制5'] = [i[4] for i in result]
df['二进制6'] = [i[5] for i in result]
df['二进制7'] = [i[6] for i in result]
df['二进制8'] = [i[7] for i in result]
df.to_excel('result.xlsx', index=False)
```
阅读全文