Read the planetary data in the text file S12_planet.txt into a Pandas DataFrame. The file will be given in wechat group. Task1, calculate the density of each planet relative to that of the Earth (i.e., the Earth’s density is 1) and add the results as a “density” column in the DataFrame. Print out the DataFrame sorted from the largest to smallest based on the planet diameter. The density formula is � � � � � � � = � / � = 3 � / ( 4 � ∗ � 3 ) density=M/V=3M/(4π∗r 3 ) Task2, read the 2 items in input as "A" and "Standard". A is a "String" and Standard is a "float". Create a Pandas Series that have "A" greater than "Standard", sorted from most to least based on the planet's "A". For example, if input is "year 500", then collect the data of planets whose years are greater than 500. Input A string and a float. Output Task1: DataFrame sorted on diameter Task2: Panda Series sorted on A. Note: You dont need to deal with rounding format. Just let the output as default like that in example. 按默认输出即可,不用特地做空数据、四舍五入或位数的处理。
时间: 2024-01-18 21:03:15 浏览: 80
battle_for_the_tank:Mod for Planetary Anhiliation,让您玩Ant vs. Bolo
我明白了,这是一个关于数据分析和 Pandas 库的问题。以下是代码实现:
Task1:
```python
import pandas as pd
import numpy as np
# 读取数据
df = pd.read_csv('S12_planet.txt', sep='\t')
# 计算相对密度
earth_density = 5.52 # 地球密度
df['density'] = 3 * df['mass'] / (4 * np.pi * (df['diameter']/2)**3) / earth_density
# 按直径降序排序
df = df.sort_values(by='diameter', ascending=False)
print(df)
```
Task2:
```python
import pandas as pd
# 读取数据
df = pd.read_csv('S12_planet.txt', sep='\t')
# 输入
A = 'year'
Standard = 500
# 筛选数据
mask = df[A] > Standard
series = df[mask].sort_values(by=A, ascending=False)[A]
print(series)
```
注意:以上代码中的 `S12_planet.txt` 数据文件需要提前下载。
阅读全文