- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个数据框 df
col1 col2 col3
a;b;c w;x 1
d;e;f x;y 2
g;h;i z;u;v 3
我想将 col1
和 col2
列中的每个字符串拆分为单独的记录,以便数据框看起来像这样
col1 col2 col3
a w 1
b x 1
c NaN 1
d x 2
e y 2
f NaN 2
g z 3
h u 3
i v 3
最佳答案
尝试组合 Series.str.split
, Series.stack
, Series.rename
, pandas.concat
, DataFrame.assign
和 DataFrame.reset_index
像这样:
df = pd.DataFrame([{'col1': 'a;b;c', 'col2': 'w;x', 'col3': 1}, {'col1': 'd;e;f', 'col2': 'x;y', 'col3': 2}, {'col1': 'g;h;i', 'col2': 'z;u;v', 'col3': 3}, {'col1': '1,2,3', 'col2': '2', 'col3': 4}])
print(df)
# col1 col2 col3
# 0 a;b;c w;x 1
# 1 d;e;f x;y 2
# 2 g;h;i z;u;v 3
# 3 1,2,3 2 4
df_new = (pd.concat([df[x].str.split('[;,]', expand=True).stack().rename(x)
for x in df[['col1', 'col2']]], axis=1)
.reset_index(level=1, drop=True)
.assign(col3=df.col3))
print(df_new)
col1 col2 col3
0 a w 1
0 b x 1
0 c NaN 1
1 d x 2
1 e y 2
1 f NaN 2
2 g z 3
2 h u 3
2 i v 3
3 1 2 4
3 2 NaN 4
3 3 NaN 4
关于python - Pandas 将列中的字符串拆分为多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55713309/
我有一个数组列表: ArrayList allText = new ArrayList(); 其内容是这样的: [Alabama - Montgomery, Alaska - Juneau, Ariz
我有一个 timestamp 格式的开始和结束时间。我想将它们分成多个时间段,例如 1 小时。 $t1 = strtotime('2010-05-06 12:00:00'); $t2 = strtot
我需要将 span10 分成 3 列,但我无法将它们排列起来。我应该在 span10 中添加一个 span12 还是使用 offset 还是??
我有一个时间序列。我想从早上 8 点到第二天早上 7:59 分成 24 小时的区 block 。我知道如何按日期分组,但我尝试过使用 TimeGroupers 和 DateOffsets 处理这个 8
我收到“街道号码邮政编码城市”形式的地址(作为字符串)。我想要做的是将街道和号码与邮政编码和城市分开。通常你可以按空格分割。但有些街道名称中也有空格,例如:“Emile Van Ermengemlaa
我有一个用户列表。其中一些用户处于第一状态,而其他用户处于第二状态。所以我想要的是将这个列表显示为首先,它按排序顺序显示存在 = 1 的用户,然后按排序顺序显示存在 = 2 的用户。这里的排序是根据用
我感觉我搜索了整个网络,但找不到一种方法将不同高度的 div 很好地划分为 3 列,就像 http://www.ing.nl 上那样 headertekst headerteksttesth
Bootstrap 3 按钮下拉菜单出现问题。你可以在这里看到我的两个例子: http://www.bootply.com/W1dLusilMk http://www.bootply.com/GGBv
我在 php 中执行以下操作 foreach($QuestionAsekd as $k => $v){ $grp_name = $v['NAME']; $groupValues[$gr
我找到了一种用pandas解析html的绝妙方法。我的数据格式有点奇怪(见下文)。我想将这些数据拆分为 2 个单独的数据帧。 注意每个单元格如何由,分隔...是否有任何真正有效的方法来分割所有这些单元
HTML 看起来像这样,但我不允许对其进行更改。我只能编写 CSS 将其变成 2 列。 Povezave www.behance.net www.kiberpipa.org www.o
假设我有以下数据框“A” utilization utilization_billable service 1
我需要将 2 个文本框拉伸(stretch)到 100% 的浏览器宽度,以及一个提交按钮。所有三个都应该在一行中,我试图拉伸(stretch)它但它没有发生......有什么想法吗? 代码: .sea
我是一名优秀的程序员,十分优秀!