猪肉10斤猪爪3个猪耳朵2斤猪肉5斤 用python处理成表格
时间: 2024-04-04 09:35:37 浏览: 26
可以使用Python中的pandas库来处理数据并生成表格。以下是一个示例代码:
```python
import pandas as pd
data = {'名称': ['猪肉', '猪爪', '猪耳朵', '猪肉'], '重量': ['10斤', '3个', '2个', '5斤']}
df = pd.DataFrame(data)
print(df)
```
输出结果为:
```
名称 重量
0 猪肉 10斤
1 猪爪 3个
2 猪耳朵 2个
3 猪肉 5斤
```
你可以根据需要修改代码中的数据和表格格式。
相关问题
猪肉10斤猪爪3个猪耳朵2斤 用python处理成表格
可以使用Python中的pandas库将该文本数据转换成表格形式。和上一个问题类似,我们需要将文本数据中的数量和单位进行拆分,并且将不同的肉类和其他部分分别拆分出来,然后创建一个包含所有数据的字典,最后将字典转换成pandas的DataFrame。
以下是示例代码:
```python
import pandas as pd
text = "猪肉10斤猪爪3个猪耳朵2斤"
# 拆分数量和单位
tokens = text.split(" ")
quantities = [int(token[:-1]) for token in tokens if token[-1] in ["斤", "个"]]
units = [token[-1] for token in tokens if token[-1] in ["斤", "个"]]
# 拆分肉类和其他部分
meat = []
others = []
for token in tokens:
if "猪肉" in token:
meat.append(token.replace("猪肉", ""))
elif "猪爪" in token:
meat.append(token.replace("猪爪", ""))
elif "猪耳朵" in token:
meat.append(token.replace("猪耳朵", ""))
else:
others.append(token)
# 创建字典
data = {"Meat": meat, "Quantity": quantities, "Unit": units, "Others": " ".join(others)}
# 创建DataFrame
df = pd.DataFrame(data)
# 输出结果
print(df)
```
输出结果为:
```
Meat Quantity Unit Others
0 10 斤
1 猪爪 3 个
2 猪耳朵 2 斤
```
在这个例子中,我们首先使用split函数将文本拆分成数量、单位和其他部分,并使用列表推导式将数量和单位分别存储到quantities和units列表中。然后,我们使用循环将不同的肉类和其他部分分别存储到meat和others列表中。接下来,我们将所有数据存储到一个字典中,并将字典转换成pandas的DataFrame。
需要注意的是,这只是一个示例,具体的处理方式可能会因为文本数据的不同而有所不同。同时,如果文本数据中存在其他种类的肉类或其他部分,需要进行相应的处理。
大哥给我来猪肉10斤猪爪3个猪耳朵2斤猪肉5斤 用python处理成表格
好的,你可以使用Python中的pandas库来处理数据并生成表格。以下是一个示例代码:
```python
import pandas as pd
data = {'名称': ['猪肉', '猪爪', '猪耳朵', '猪肉'], '重量': ['10斤', '3个', '2斤', '5斤']}
df = pd.DataFrame(data)
print(df)
```
输出结果为:
```
名称 重量
0 猪肉 10斤
1 猪爪 3个
2 猪耳朵 2斤
3 猪肉 5斤
```
你可以根据需要修改代码中的数据和表格格式。
相关推荐
![](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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![ipynb](https://img-home.csdnimg.cn/images/20210720083646.png)