如何将一个熊猫数据框架的多列与一个系列相结合?
我有一个数据框架实际上有20列以上。下面的示例给出了4列。每列的行数量相等。如何将仅具有一列的新数据框架转换为新的数据帧(如下所示的Exmaple)。我将使用新的组合数据框架来计算一些指标。如何为此编写一个整洁有效的代码?太感谢了!
data={"col1":[1,2,3,5], "col_2":[6,7,8,9], "col_3":[10,11,12,14], "col_4":[7,8,9,10]}
pd.DataFrame.from_dict(data)
I have a data frame that has actually more than 20 columns. The example below give 4 columns. each column has equal number of rows. How to convert to a new dataframe(exmaple shown below) which has only one columns. I will use the new combined dataframe to calculate some metrics. How do I write a neat and efficient code for this? Thank you so much!
data={"col1":[1,2,3,5], "col_2":[6,7,8,9], "col_3":[10,11,12,14], "col_4":[7,8,9,10]}
pd.DataFrame.from_dict(data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将 DataFrame 转换为 numpy 数组,并使用 将其展平
ravel
方法。最后,用结果构造一个 Series(或 DataFrame)。输出:
You can convert the DataFrame to a numpy array and flatten it using the
ravel
method. Finally, construct a Series (or a DataFrame) with the result.Output:
如果您从字典开始,请使用 code>:
Else,
ravel< /code>
the underlying numpy array:
Output:
Depending on the computation to perform, you might not even need to instantiate a DataFrame/Series and stick to the array:
Output:
array([ 1, 2, 3、5、6、7、8、9、10、11、12、14、7、8、9、10])
If you start from your dictionary, use
itertools.chain
:Else,
ravel
the underlying numpy array:Output:
Depending on the computation to perform, you might not even need to instantiate a DataFrame/Series and stick to the array:
Output:
array([ 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 14, 7, 8, 9, 10])
尝试使用
melt
Try with
melt