简体   繁体   English

pivot 列名称和索引 pandas df 到列本身的有效方法,对应的值作为第三列?

[英]Efficient way to pivot columns names and index in pandas df to columns themselves, with corresponding values as third column?

I have a very large dataset in python that came from a net CDF file.我在 python 中有一个来自网络 CDF 文件的非常大的数据集。 The column names are latitudes, and the indexes are longitudes.列名是纬度,索引是经度。 For each index/column in the database there is az value I am interested in. I want a new dataframe with the columns ['Latitude','Longitude','Z'] .对于数据库中的每个索引/列,我感兴趣的 az 值。我想要一个新的 dataframe 列['Latitude','Longitude','Z'] I was able to come up with one solutions using itertools, but I my dataframe dimensions are (7200,14400) giving me 103,680,000 values to iterate over.我能够使用 itertools 提出一种解决方案,但我的 dataframe 尺寸为 (7200,14400) 给我 103,680,000 个值进行迭代。 Is there a more efficient way to do this.有没有更有效的方法来做到这一点。 I provide here a sample input and output to make testing easy.我在这里提供了一个示例输入和 output 以简化测试。 Is there a pivot function in pandas or another efficient way to solve this problem? pandas 中是否有 pivot function 或解决此问题的其他有效方法?

#import libraries
import numpy as np
import pandas as pd
import itertools

#Create Sample Data
columns=['a','b','c']
rows=['1','2','3']
d_base=np.array([0.1,0.2,0.3])
data=np.tile(d_base,(3,1))

#create df
df=pd.DataFrame(data,columns=columns,index=rows)

df


Out[]
     a    b    c
1  0.1  0.2  0.3
2  0.1  0.2  0.3
3  0.1  0.2  0.3

This is the solution that works but is slow.这是有效但速度慢的解决方案。

#iterate all combinations of columns and rows
col_index_pairs=list(itertools.product(columns, rows))

desired_output=pd.DataFrame()

#lookup the value of each possible pair in the original dataframe and put it into a new one.
for item in col_index_pairs:
    desired_output[item]=[item[0],item[1],df.loc[item[1],item[0]]]
    
desired_output=desired_output.T
desired_output.columns=['Latitude','Longitude','Z']
desired_output

Out[]: 
       Latitude Longitude    Z
       a         1           0.1
       a         2           0.1
       a         3           0.1
       b         1           0.2
       b         2           0.2
       b         3           0.2
       c         1           0.3
       c         2           0.3
       c         3           0.3
    

You may check with melt你可以检查melt

s = df.reset_index().melt('index')
Out[18]: 
   index variable  value
0      1        a    0.1
1      2        a    0.1
2      3        a    0.1
3      1        b    0.2
4      2        b    0.2
5      3        b    0.2
6      1        c    0.3
7      2        c    0.3
8      3        c    0.3

暂无
暂无

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

相关问题 从 Pandas df 列名和值创建新列 - Creating new columns from pandas df column names and values 大熊猫:通过结合索引和列名称来对列进行旋转和展平 - Pandas: pivot and flatten columns by combining index and columns names 寻找使用字典向pandas df添加动态列的有效方法 - looking for efficient way to add dynamic columns to pandas df using dictionary 如何使用pandas创建新的df列以使用正则表达式获取列名和值? - How to make new df columns using pandas to get column names and values using regex? 如何 pivot df 将列值的计数放入新列 - How to pivot df to get counts of column values into new columns 保存 pandas pivot_table 以包含索引和列名称 - Save pandas pivot_table to include index and columns names 基于df.columns和Series.index名称将pandas.Dataframe与pandas.Series合并的最佳方法是什么? - what is the best way to merge pandas.Dataframe with pandas.Series based on df.columns and Series.index names? 如何获取包含与索引对应的特定值的列列表作为 pandas dataframe 中的新列? - How to get list of columns containing specific values corresponding to a index as a new column in pandas dataframe? 从另一列pandas df分配值的有效方法 - Efficient way to assign values from another column pandas df 更有效的方法来表示在熊猫数据框中将列子集居中并保留列名 - More efficient way to mean center a sub-set of columns in a pandas dataframe and retain column names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM