如何将 Pandas 数据帧列转换为图像数组,即 Python 中形状为 (n,n) 的 numpy 数组?

如何解决如何将 Pandas 数据帧列转换为图像数组,即 Python 中形状为 (n,n) 的 numpy 数组?

假设我的数据框在一列中有 750 行,我想将该列转换为 (20,20) numpy 数组的图像数组。如何做到这一点?

EDIT1:我想将 ax.contourf (x,y,z) 的数组用作 z。我通过执行 x,y=np.meshgrid(df3.x,df.y) 得到了 x,y 现在我想将另一列转换为 (n,n) 数组以使用 z 参数改变轮廓内的颜色。

EDIT2:x,z Data to plot contour plot

解决方法

鉴于您已经将 x 和 y 作为列,我会像这样使用 pivot_table

df = pd.DataFrame(
  columns=["z","x","y"],data=[
    [-0.00222,38,46.1],[-0.00374,30.5,68.4],[0.001043,36,86.9],[0.003473,52.9,95.3],[0.001382,50.6,80.3],[-0.00486,37,92.7],[-0.00016,29.8,69.5],[0.001368,34.7,27.2],[-0.0016,37.5,63],[-0.00181,45.5,92],[0.003635,53.6,82.4],[-0.00363,46.7,91.1],[0.002253,54.1,97.8],[-0.00089,43.8,87],[-0.00115,41,62.7],[-0.001,47.5,23.2],[0.001332,61.2,4.7],[-0.00361,45.9,13.9],[0,47.4,32],66.6],[0.000891,53.2,7.3],[-0.00133,56.1,15.8],[-0.00045,43.6,19.3],[0.004365,68.8,94.2],[-0.00355,71.1,76.1],71.3,64.8],[-0.00415,65.7,59.4],[0.003881,72.3,47],[-0.00727,54.7,[0.001848,63.3,81.3],[0.002518,69.7,93.8],[-0.00252,65.2,84],[-0.0025,55.8,67.5],68.2,94.5],[0.008311,76.4,85.4],[0.000208,35.4,70.5],[-0.00021,45,[-0.00189,32,75.5],[0.000219,32.3,38.3],[0.001522,33.4,61.7],37.6,37.1],[0.0016,47.6,32.5],[0.000595,59.1,7.1],63.1,5],[0.00212,55.3,26.5],[0.002495,64.7,27.4],[0.00197,79.4,24.2],[0.006561,75.2,38.9],[0.011262,82.3,53.3],[0.019798,88.7,25.6],[-0.0198,78.9,45.2],[0.000602,22.6,74.7],[-0.00082,29,93.5],[0.002707,95],[0.000497,27.7,45.4],[-0.00131,19.3,28.7],[0.000152,19.2,[-0.00015,23.5,64.5],[0.001311,31.7,41.9],[0.004164,54.8,28.4],[-0.00185,57.8,[0.004771,69.3],[0.010728,87.7,92.3],[-0.0063,81.6,71.6],[0.079499,84.6,52.1],[-0.07294,78.2,[-0.00656,80.6,70.7,67.2],[0.00541,76.1,[0.003548,93.2],74.9,84.8],68.6,60],[0.000164,70,17.2],[0.005518,78.8,16.3],[-0.00466,64,27.7],[0.000619,66.5,46],[-0.00552,71.6,77.9],[-0.00027,72,50.7],[0.002583,46,87.2],[0.007159,69.6,75.6],77.1,84.9],[-0.00729,92.4],75],[-0.00011,74.7,[0.011137,90.9,22.6],77.8,41.2],[-0.00186,14.3,38.6],28,61.2],[0.001303,38.9,55.7],[0.002049,52.4,41.8],[-0.00205,41.9,51.3],[-0.00055,70.2],71.8,[-0.00477,65.8,67.8],75.5,84.2],[-0.00541,74.5,72.2],85.7],[-0.00568,54.5],[0.001221,46.9,3.5],58.2,14.9],[0.000446,45.2,34],68.4,37.4],55.1],67.6,61.8],[0.022626,94.2,84.6],[-0.0026,45.8,69.8],[0.002565,55.7,74.9],68,91.3,70.7],92.6,[-0.01073,74.6,74.6],[0.005318,87.2,92.6],79.8,[0.017925,98.1,78],[0.000668,34.4,91.2],36.2,92.9],[0.000885,38.2,75.9],[-0.00094,21.7,[0.000939,30.2,31.3],[-0.00088,37.9,6.2],27.4,36.7],[-0.00048,39.8,[0.005303,63.2,95.9],[0.001901,61.8,[0.000652,22,89.1],[0.003699,33.6,[0.005125,60.3,58.1],38.5],65,60.2],60.9,39.1],[0.004152,67.7,27.1],[0.003441,77,9.7],[-0.00344,28],70.9,49.7],[0.012118,23.8],[0.000366,25.2,93.3],[-0.00212,58,94.4],[-0.00225,42.4,52,58.7,53.9,[-0.00388,58.9,45.1],[0.006956,67,48.2],[-0.007,51.9,[-0.00416,39.1,29.8],[-0.00253,19.8,44],[-0.00044,30.4],77.4],53.1,66.8],41.5,35.2],66],64.1,96],68.3],62.9,9.4],73.4,[0.025256,94.3,13.3],[-0.01084,16.4],[0.010991,56.3],[0.010999,89.2,87.5],72.5,80.4],[0.000107,69.6],73,[0.002273,56.7,83.3],[-0.00035,23.4,77.6],[0.000353,44,[-0.00034,34.3,84.5],[-0.00122,39.4,59.7],[0.001219,47.9,77.1],[0.001001,43.9,31.4],[0.000555,45.1,[0.003857,58.6,67.5,96.5],[-0.00059,73.7],[-0.00201,49.5,[-0.00078,54,17],66.5],91.4],70.4,79.8],69.2,[0.000271,68.3,36.2],11.2],82.8,15.4],[-0.00098,75.9,33.5],90.5,42.1,10.4],[0.001808,48.4,[0.007266,69.4,26.6],78.6,[0.000989,80.3,28.1],[-0.00443,74.8,28.9],[0.001417,10.9,9.2,46.3],32.9,49.4],[-0.0005,8.6,48.1],[0.000277,28.1,32.2],[-0.00232,12.5,23.3],[0.000991,[0.000818,30.7,18.7],[0.000479,39.6,54.9],[0.00222,48.8,[-3.93E-05,53.7,29.9],[-0.00166,35.1,[-0.00116,17.8,[0.002461,[-0.00014,46.2,4],54.5,19.6],[3.93E-05,50.9],[0.001251,73.6,68.1],[-0.01036,75.6,[0.005419,66.3],74.4,74.4],72.5],55.2],[0.016548,90,[0.067381,86.9,79.2,[-0.0047,73.7,[0.005681,78.1,19.1],77.2,58.7],[-0.0045,66,67.4],[-0.00246,53,[0.007227,66.9,66.2],90.5],86.4,29.5],[0.009515,97.7,[0.016818,89.1,76.8],[-0.01212,80.7,68.6],[0.013629,85.6,96.9],81,71,82.1],24.5],76.7,79,53.6],29.7,72.6],64.2],44.1,44.9],[-0.0027,37.8,17.8],[0.000336,44.3,44.4,12.1],[-0.00024,29.9,88.2],[0.000589,23.2,33.9,42.8],[-0.0006,5.7,45.7],23,25.9,4.3],[-0.0042,40.7,68],[0.001154,43.4,55.2,42,88.7],[-0.00138,31.2,16.8,[0.002526,41.4,72.9],55.9,75.3],[0.010668,77.5,95.7],87.9,30.8],[0.003771,43.3,[-0.00271,91.7],[0.001051,14.2,54],[0.003464,43.5,67.1],47.1,57.5],[0.00201,56.5,[0.00187,95.6],75.8,67.2,69.9],73.9,54.6],[-0.01126,57],[0.004701,81.4,34.4],[0.000232,40.6],26.3,26.8],[-0.00075,4.1,51.5],[-0.00062,8.5,83.6],[0.002706,46.6,93.9],68.7,58.8],47.5],[-0.00149,30.1,90.6],[0.002097,36.6,65.4],[6.53E-05,34.2,30.3],[0.002044,54.9,4.1],10.7],68.9,73.8],82,82.7],79.5,[0.016944,92.2,85.5],[-0.00634,96.2],[-0.00377,69],59.7,66.9],74,83.4],86.3],80.9,93.6],79.1,51.9],55.5],[-0.00733,75,71.3],66.3,71],[0.004664,68.1,69.9,65.6],74.3,66.7,93],[0.002901,72.3],[0.00443,72.8,[0.006342,77.6,88.1],57.8],66.4,35.5],68.7],53.7],[0.007592,81.5,11.3],[-0.01655,29.3],61.3,53.9],69,49.9],88.9,86.1],50.1,[0.004326,71.7,41.5],79.3,64.1],[-0.00843,92.7,76.2],[0.001253,59.4,56,33.7],65.4,5.3],61.5,14.2],[0.001267,37.9],77.4,72.7,54.2],[0.026359,91.4,76.8,6.1],[-0.00022,29.6,36.5,[0.011496,64.4,39.8],90.3,[0.000367,14.6,[0.001327,28.8,63.3],78.9],42.2,27.2,85.1],56.6],[0.001328,26.1,42.3,[0.00011,44.9,[0.0045,38.8],62,75.3,18.8,55.4],[-0.00039,13.4,46.5,89.7],53.8,85.7,89.4],75.1,14.6],67.3,56.9],[0.003712,82.5,87.8],80.2,70.4],70.1,79.9,85.8],79.7,72.1,76.6,70.6,73.5],78.7,65],11.5],73.3,74.2,57.6],[-0.01665,[0.004537,71.4],27.8,47.3],53.5,40.4,51],[0.002109,52.5,52.7,25.7],55.6,[0.006996,67.1,43.9],62.7,74.1,44.3],60.9],[0.004045,73.5,68.9],[1.33E-05,22.2,85.8,78.6],72.6,13.7],51.7,65.3],[-0.00256,68.8],73.2],78,36.4],[0.000981,80.4,[-0.00308,87.4,[-0.00532,86.8,76.5],81.8,29.6],81.9,47.8],39.3,46.3,59.6],[0.001272,87.3],[0.002715,64.9,[-0.00125,63,87.9],73.4],[-0.00074,16.6],60.6,6.5],85.4,66.6,64.2,[0.005312,[-0.00531,64.5,65.5],51.5,58.3,30.5],52],83,60.3],[0.202542,97,44.5],10.1,[-0.00037,7.9,49.3],80],24.3,58.2],35.9,33.5,86],41.6,89.6],[0.00305,37.1,54.3,[0.000738,37.3],90.2],83.7],71.9],70.3,75.8],82.1,60.8],[0.0842,88.8,62.3],[0.013107,91.5,80.6],20.8],71.4,70.2,78.5],89.4,92.4,[0.061574,84.9,[0.011696,25.9],57.9],[0.003631,12.2],59.8,69.8,64.7],53.4],46.4],[0.020699,86.3,81],[-0.01114,75.2],84.5,71.5,86.4],[0.010836,86.6,[-0.01363,[0.002794,77.7],76.5,[0.004204,54.4,[0.003075,50.1],[-0.00427,40.3,44.7,84.3],51.2,52.8,63.9],51.6,65.1],50.8,21.8],55,[-0.00575,33,7.5,[-0.00191,7.1,20.6],[0.000531,2.9,43.7],[0.001656,89.3],85,13.1],[-0.02636,78.4,36.8],78.2],71.1],75.7,43],78.3,19.7],24.8],11.8],20.2],60.6],57.2],67.9,79.1],37.3,[-0.00054,14.3],[0.000545,37.4,36.1],[-0.00152,65.9],63.6],40.7],[0.000823,20],[-0.00067,23.5],[0.003187,[0.00506,99.3],[0.00755,[-0.03587,62.6],[0.009374,93.8,[0.010363,95.3,[-0.01793,82.2,[0.000388,20.7,[-0.0019,24.7],[0.000814,12.2,51.8],31.1,[0.002144,48.1,84.4,94.1],56],38.1,[-0.0013,25.8,57.4,90.8],[0.003115,43.3],[-0.00066,17.1,43.1],[0.000662,10.3,56.7],27.6,59.3],[-0.00303,92.1],32.8,90.9],[-0.00347,39.5,41],33.4],62.5,[0.000648,62.2,31],[0.0265,95.9,[0.001552,37.7,78.8],39,[0.00853,70.5,9.6,14.5],[0.001809,26.4,18.8],40.1,6.8],39.2],65.7],[0.00622,14.8],[-0.00065,58.5,7.9],72.2,[0.016655,15.5],72.4,49.6],[0.009959,76.3,57,45.6,47.6],63.7,63.5],76.4],[0.000948,23.1,[0.002915,31.7],67.8,9.5],[0.006188,26],[0.030789,86.1,74.3],91.5],[-0.00263,97.5],[-1.33E-05,6.4,8.2],60,61.5],8.5],[0.000755,21.1,58.5],6,52.4],[0.008524,60.2,62.9],[0.040304,98,]
)
df2 = df.pivot_table("z",index="y",columns="x",aggfunc="sum").fillna(0)
array = df2.sort_index(ascending=False).values

pivot_table 的美妙之处在于它可以对您的 x/y 值进行排序。

不过要小心,你的 y 值会上升(从上到下),这与你在图片中想要的相反。您必须以反向模式重新排序索引(因此使用 sort_index 命令)。

这里还有一个假设:您的 x 和 y 的值是线性的,或者您不想绘制任何线性的图。事实上,您的数据样本不是(例如,您的第一个离散 y 值是 3.5、4、4.1);您根本无法将这种坐标用于网格格式中的任何内容。

鉴于您的数据坐标都是带一位小数(最多)的浮点数,您可以通过这种方式完成数据:

all_xs = all_ys = range(0,1001) # get all discrete xs and ys values (on an integer basis)
missing_xs = list(set(all_xs) - set(df['x']*10)) #find all missing xs
missing_ys = list(set(all_ys) - set(df['y']*10)) #find all missing ys
dummy_df = pd.DataFrame(0,index=missing_ys,columns=missing_xs) #construct a dummy dataframe with those xs and ys (filled with zeros)
df2.index *= 10 #convert your indexes and columns of the previous df2 to integers
df2.columns *= 10
df3 = df2.append(dummy_df).fillna(0) #append the dataframes,which will create the missing columns and index and fill all empty values with zeros
df3.sort_index(inplace=True,ascending=False) #resort the index (ie ys values)
df3.sort_index(axis=1,inplace=True,ascending=True) #resort the columns (ie xs values)
print(df3.shape) #make sure the shape is a square array
array = df3.values #extract the numpy array
,

您可以将列数据转换为 numpy 数组并将其重塑为您所需的尺寸:

In [57]: df = pd.DataFrame({"A": [1,2,3,4,5,6],"B": [3.0,4.5,9,8,6]})                                                                                                                                 

In [58]: df                                                                                                                                                                                                 
Out[58]: 
   A     B
0  1   3.0
1  2   4.5
2  3   9.0
3  4   8.0
4  5  36.0
5  6   6.0

In [59]: res= np.array(df['B'].values.tolist())                                                                                                                                                             

In [60]: res                                                                                                                                                                                                
Out[60]: array([ 3.,9.,8.,36.,6. ])

In [61]: res.reshape(3,2)                                                                                                                                                                                   
Out[61]: 
array([[ 3.,4.5],[ 9.,8. ],[36.,6. ]])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res