【问题标题】:dictionary or sub df from df字典或来自 df 的子 df
【发布时间】:2018-08-14 07:03:38
【问题描述】:

我在一般的编程方面是全新的,所以请解释一下。 总体目标:我正在处理 x,y,z 数据。我想在不影响平均值的情况下减少每个单元格中的点数(可能有不同的大小取决于项目)假设为 50。 问题:我有 x,y,z,binnumber 的 df 并且我想生成任一字典(例如 binnumber:[x,y,z],[x,y,z] .....这是在这个 bin ),或者一些我可以作为 df 使用的子数据集,以便我可以使用。 我做了什么:

    `# import the data
import pandas as pd
import numpy as np
from scipy.stats import binned_statistic_2d
inputpath=input("write the file path:")
Data = pd.read_csv(inputpath,  index_col=False, header= None, names =
['X','Y', 'Z'],skip_blank_lines=True) # file name , index =False means 
without index , names are the columns names
Data = pd.DataFrame(Data)

# creating the grid cells
min_x = int(min(Data['X'])) 
max_x = int(max(Data['X'])+1)
min_y = int(min(Data['Y']))
max_y = int(max(Data['Y'])+1)
bin_size = float(input('write the cell size:'))
bx= int(((max_x-min_x)//bin_size)+1) 
by=int(((max_y-min_y)//bin_size)+1)
xedges = np.linspace(min_x, max_x, bx, dtype=int) 
yedges = np.linspace(min_y, max_y, by, dtype=int) 

# assign the data to the cells
count, x_edge,y_edge,binnumber= binned_statistic_2d(Data['X'], Data['Y'], 
Data['Z'],bins=(xedges, yedges))
Data['binnumber']= binnumber
# sub sets
subsets = dict(Data.groupby('binnumber'))
print (subsets)

这不起作用... 另一种解决方案是处理细胞本身,但它也不起作用。

cells= {}
for i in xedges:
    for j in yedges:
        cells[str(i),str(j)]=[]
print(cells.keys())
for x in Data.X:
for y in Data.Y:
    for z in Data.Z:
        for k,v in cells.keys():
            if x>= int(k[0]) and x < int(k[0]) +1 and y>= int(k[1]) and y 
                 < int(k[1]) +1:
                k=(x,y,z)
            else:
                cells=('0')


print(cells) 

感谢您的帮助。

【问题讨论】:

    标签: python pandas numpy matplotlib scipy


    【解决方案1】:
    import the data
    import pandas as pd
    import numpy as np
    from scipy.stats import binned_statistic_2d
    
    inputpath=input("write the file path:")
    Data = pd.read_csv(inputpath,  index_col=False, header= None, names =
    ['X','Y', 'Z'],skip_blank_lines=True) # file name , index =False means 
    without index , names are the columns names
    Data = pd.DataFrame(Data)
    
    # creating the grid cells
    min_x = int(min(Data['X'])) 
    max_x = int(max(Data['X'])+1)
    min_y = int(min(Data['Y']))
    max_y = int(max(Data['Y'])+1)
    bin_size = float(input('write the cell size:'))
    bx= int(((max_x-min_x)//bin_size)+1) 
    by=int(((max_y-min_y)//bin_size)+1)
    xedges = np.linspace(min_x, max_x, bx, dtype=int) 
    yedges = np.linspace(min_y, max_y, by, dtype=int) 
    
    # assign the data to the cells
    count, x_edge,y_edge,binnumber= binned_statistic_2d(Data['X'], Data['Y'], 
    Data['Z'],bins=(xedges, yedges))
    Data['binnumber']= binnumber
    
    # making dictionary with >>> binnumber: all associated points......
    Data['value'] = list(zip(Data['X'], Data['Y'], Data['Z']))
    d = defaultdict(list)
    for idx, row in Data.iterrows():
        d[row['binnumber']].append(row['value'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 2014-10-28
      • 1970-01-01
      • 2021-10-31
      • 2014-03-23
      • 1970-01-01
      • 2019-03-12
      • 2016-10-08
      相关资源
      最近更新 更多