创建数据表
设置列名,以及类型
all_data = pd.DataFrame(data=None, columns=['lon', 'lat', 'time', 'rank', 'wight', 'day'])
all_data['lon'] = all_data['lon'].astype('float')
all_data['lat'] = all_data['lat'].astype('float')
all_data['time'] = all_data['time'].astype('string')
all_data['rank'] = all_data['rank'].astype('int')
all_data['wight'] = all_data['wight'].astype('float')
all_data['day'] = all_data['day'].astype('int')
若要指定数据,一定要注意字典里的数据应为列表
df = pd.DataFrame(data={'lon': [row['lon']], 'lat': [row['lat']], 'time': [row['time']], 'rank': [1],'wight': [0], 'day': [0]})
遍历数据表
index为行号,row返回一行的引用,当修改row时,原数据表的内容也一起修改
for index, row in day_higMidPos.iterrows():
总的
# 判断一个值是否在某一列中存在
# Replace 'column_name' with the name of the column you want to check and 'specified_value' with the value you want to check for
df['column_name'].isin(['specified_value'])
# 使用两个条件进行筛选
# Replace 'column1_name' with the name of the first column you want to filter and 'specified_value1' with the value you want to filter for in the first column
# Replace 'column2_name' with the name of the second column you want to filter and 'specified_value2' with the value you want to filter for in the second column
df_filtered = df[(df['column1_name'] == 'specified_value1') & (df['column2_name'] == 'specified_value2')]
# 在一列中,删除值大于等于5的行
# Replace 'column_name' with the name of the column you want to filter and '5' with the value you want to filter for
df = df.drop(df[df['column_name'] >= 5].index, axis=0)
# 对某一列数据求和
# Replace 'column_name' with the name of the column you want to sum
sum_of_column = df['column_name'].sum()
# 将某一列赋值为同一个值
# Replace 'column_name' with the name of the column you want to set and 'specified_value' with the value you want to set the column to
df['column_name'] = 'specified_value'