python - 如何分隔数据框中的两行
问题描述
我有一张多伦多社区的表格,在某些情况下,您会在同一个盒子中找到两个或三个社区,所以同一行(对于同一个自治市镇)我想将它们分成两行如何做请!我正在使用熊猫。谢谢
解决方案
你应该用你预期的输入和输出的例子来充实这个问题,但我认为你想要这样的东西使用pandas.DataFrame.explode
import pandas as pd
df = pd.DataFrame(dict(
city=['Seattle'],
hood=[['SoDo', 'Maple Leaf', 'View Ridge', 'North Gate', 'SLU']],
))
print('df before exploding:\n', df)
df = df.explode('hood')
print('\ndf after exploding:\n', df)
输出:
df before exploding:
city hood
0 Seattle [SoDo, Maple Leaf, View Ridge, North Gate, SLU]
df after exploding:
city hood
0 Seattle SoDo
0 Seattle Maple Leaf
0 Seattle View Ridge
0 Seattle North Gate
0 Seattle SLU
Process finished with exit code 0
编辑:OP回应:
是的,这正是我想要的,但我有一个问题 m 盒子是这样的:df before exploding: hood =[' SoDo, Maple Leaf, View Ridge, North Gate, SLU'] 所以我尝试使用 explod 但它不是可能是因为 , 是一个字符串字符
如果是这种情况并且它只是一个字符串,那么还有以下额外步骤:
import pandas as pd
df = pd.DataFrame(dict(
city=['Seattle'],
hood=[['SoDo, Maple Leaf, View Ridge, North Gate, SLU']],
))
print('df before exploding:\n', df)
df['hood'] = df['hood'].apply(lambda x: x[0].split(', '))
print('\ndf after splitting string in list:\n',df)
df = df.explode('hood')
print('\ndf after exploding:\n', df)
输出:
df before exploding:
city hood
0 Seattle [SoDo, Maple Leaf, View Ridge, North Gate, SLU]
df after splitting string in list:
city hood
0 Seattle [SoDo, Maple Leaf, View Ridge, North Gate, SLU]
df after exploding:
city hood
0 Seattle SoDo
0 Seattle Maple Leaf
0 Seattle View Ridge
0 Seattle North Gate
0 Seattle SLU
Process finished with exit code 0
推荐阅读
- java - 在 iText Java 中重命名 Radio 组
- r - 在 R 中拟合指数衰减模型
- elasticsearch - 在 elasticsearch 中添加新的同义词
- reactjs - 多次调用 onClick 的 useDarkMode 钩子
- angular - 对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。(Spring Security & Angular)
- python - 创建一个列,该列是每行中具有最大长度的字符串 - Pandas Dataframe
- office-js - Outlook for Mac 16.38 加载项对话框无法向父级发布消息
- pandas - 从第一张表中提取信息时如何更新不同的谷歌表?
- r - R:data.table 使用 for 循环来处理多列
- postgresql - 无法在 AWS CodeBuild 中访问 Postgres Docker 容器