Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何按列值的范围将单个数据帧拆分为多个数据帧?_Python_Python 3.x_Pandas - Fatal编程技术网

Python 如何按列值的范围将单个数据帧拆分为多个数据帧?

Python 如何按列值的范围将单个数据帧拆分为多个数据帧?,python,python-3.x,pandas,Python,Python 3.x,Pandas,首先,我意识到这个问题已经以许多不同的形式被问了很多次,但很多答案只是给出了解决问题的代码,而没有解释代码的实际功能或工作原理 我有一个巨大的电话号码和区号数据集,我已经用python将其加载到数据框中,以便进行一些处理。在进行处理之前,我需要将单个数据帧拆分为多个数据帧,这些数据帧包含特定区域代码范围内的电话号码,然后我可以对其进行更多处理。例如: +---+--------------+-----------+ | | phone_number | area_code | +---+--

首先,我意识到这个问题已经以许多不同的形式被问了很多次,但很多答案只是给出了解决问题的代码,而没有解释代码的实际功能或工作原理

我有一个巨大的电话号码和区号数据集,我已经用python将其加载到数据框中,以便进行一些处理。在进行处理之前,我需要将单个数据帧拆分为多个数据帧,这些数据帧包含特定区域代码范围内的电话号码,然后我可以对其进行更多处理。例如:

+---+--------------+-----------+
|   | phone_number | area_code |
+---+--------------+-----------+
| 1 | 5501231234   | 550       |
+---+--------------+-----------+
| 2 | 5051231234   | 505       |
+---+--------------+-----------+
| 3 | 5001231234   | 500       |
+---+--------------+-----------+
| 4 | 6201231234   | 620       |
+---+--------------+-----------+
进入

我知道这应该是可能的使用熊猫(特别是groupby和一个系列对象,我认为),但在互联网上我可以找到的文档和例子有点太模糊或稀疏,我无法遵循。也许有比我尝试的方式更好的方法来实现这一点?

您可以使用
bin
区域列,然后使用标签将数据分组并存储在字典中。最后打印每个关键点以查看数据帧:

bins=[500,550,600,650]
labels=['500-550','550-600','600-650']



您还可以通过使用
&
|
运算符链接多个条件来选择数据帧中的行来完成此操作

  • df1选择区域代码在500-550之间的行

  • df2选择区域代码在600-650之间的行


df=pd.DataFrame({'phone_number':[5501231234150512312450012341224620131234],
“区号”:[550505500620]},
列=[‘电话号码’、‘区号’])

df1=df[(df['area_code']>=500)和(df['area_code']=600)和(df['area_code']难以置信的简单和高效。工作起来很有魅力,我也明白发生了什么哈哈,谢谢!@Tom很高兴我能帮忙。快乐编码:)
area-codes (600-650)
+---+--------------+-----------+
|   | phone_number | area_code |
+---+--------------+-----------+
| 1 | 6201231234   | 620       |
+---+--------------+-----------+
bins=[500,550,600,650]
labels=['500-550','550-600','600-650']
d={f'area_code_{i}':g for i,g in 
  df.groupby(pd.cut(df.area_code,bins,include_lowest=True,labels=labels))}

print(d['area_code_500-550'])
print('\n')
print(d['area_code_600-650'])
    phone_number  area_code
0    5501231234        550
1    5051231234        505
2    5001231234        500


   phone_number  area_code
3    6201231234        620
df1
phone_number  area_code
0    5501231234        550
1    5051231234        505
2    5001231234        500

df2
phone_number  area_code
3    6201231234        620