简体   繁体   English

如何从一个 zip 在 dataframe 中创建 2 列

[英]How to create 2 columns in a dataframe from one zip

I wish to turn the zips in the 3rd columns into 2 different columns with either number as seen in df2, how would I do this?我希望将第 3 列中的拉链变成 2 个不同的列,其中任一数字在 df2 中看到,我该怎么做?

import pandas as pd
import numpy as np
data = pd.DataFrame(([[1, 2, (3,5)], [4, 5, (6,4)], [7, 8, (9,1)]]), columns = ['a', 'b', 'c and d'])
print (data)

the desired output:所需的 output:

data2 = pd.DataFrame(([[1, 2, 3,5], [4, 5, 6,4], [7, 8, 9,1]]), columns = ['a', 'b', 'c', 'd'])
print(data2)

You can pop the column so it's removed and then use the DataFrame constructor on the list of tuples.您可以pop列以将其删除,然后在元组列表上使用DataFrame构造函数。 Join the results.加入结果。

res = pd.concat([data, pd.DataFrame(data.pop('c and d').tolist(), columns=['c', 'd'])], axis=1)

   a  b  c  d
0  1  2  3  5
1  4  5  6  4
2  7  8  9  1

More straight-forward, you can assign the elements of the tuples with the Series.str accessor, then delete the column.更直接地说,您可以使用 Series.str 访问器分配元组的元素,然后删除该列。

data['c'] = data['c and d'].str[0]
data['d'] = data['c and d'].str[1]
data = data.drop(columns='c and d')

Another solution:另一种解决方案:

data[["c", "d"]] = data["c and d"].apply(pd.Series)
data = data.drop(columns="c and d")
print(data)

Prints:印刷:

   a  b  c  d
0  1  2  3  5
1  4  5  6  4
2  7  8  9  1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM