pandas如何对1000元/平方米这种排序呢
时间: 2023-08-10 16:05:31 浏览: 46
如果您有一个包含1000元/平方米的`pandas`数据框,您可以使用`sort_values()`方法来排序。在默认情况下,它会按升序对数据进行排序,所以您需要指定`ascending=False`来按降序排序。以下是示例代码:
```python
import pandas as pd
df = pd.DataFrame({'price_per_sqm': ['200元/平方米', '1000元/平方米', '500元/平方米']})
df['price_per_sqm'] = df['price_per_sqm'].str.extract('(\d+)').astype(int)
sorted_df = df.sort_values(by='price_per_sqm', ascending=False)
print(sorted_df)
```
上述代码将提取价格信息并将其转换为整数类型。然后,使用`sort_values()`方法并指定`by`参数为`price_per_sqm`,以对数据进行排序。最后,使用`print()`函数打印排序后的数据框。
相关问题
pandas对excel排序
要使用Pandas对Excel进行排序,可以使用Pandas的sort_values()方法。
首先,需要使用Pandas的read_excel()方法将Excel文件读入DataFrame中。然后,使用sort_values()方法对DataFrame进行排序,最后使用to_excel()方法将排序后的DataFrame写回Excel文件中。
以下是一个示例代码,假设要将Excel文件中的Sheet1按照“列1”进行升序排序,然后将排序后的结果写回Excel文件中。
```python
import pandas as pd
# 读入Excel文件
df = pd.read_excel('file.xlsx', sheet_name='Sheet1')
# 按照“列1”进行升序排序
df_sorted = df.sort_values(by='列1', ascending=True)
# 将排序后的结果写回Excel文件中
df_sorted.to_excel('file_sorted.xlsx', sheet_name='Sheet1', index=False)
```
其中,by参数指定按照哪一列进行排序,ascending参数指定排序方式,True表示升序,False表示降序。在to_excel()方法中,index参数指定是否将DataFrame的行索引写入Excel文件中,False表示不写入。
pandas对Excel进行排序
可以使用pandas库中的sort_values()方法对Excel表格进行排序。具体步骤如下:
1. 使用pandas的read_excel()方法读取Excel表格数据,并将其存储在一个DataFrame对象中。
2. 使用sort_values()方法对DataFrame对象进行排序,可以指定排序的列及排序方式(升序或降序)。
3. 使用pandas的to_excel()方法将排序后的数据写入Excel表格中。
示例代码如下:
```python
import pandas as pd
# 读取Excel表格数据
df = pd.read_excel('data.xlsx')
# 对DataFrame对象按照'列名'进行升序排序
df_sorted = df.sort_values(by='列名', ascending=True)
# 将排序后的数据写入Excel表格
df_sorted.to_excel('sorted_data.xlsx', index=False)
```
其中,by参数指定按照哪一列进行排序,ascending参数指定排序方式,True为升序,False为降序。to_excel()方法中的index参数指定是否将DataFrame的索引写入Excel表格中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)