找到 10786 篇文章 关于 Python
144 次查看
假设您有一个数据框,将索引正向和负向移动两个周期的结果是,将索引正向移动三个周期 Id Age 2020-01-01 00:00:00 NaN NaN 2020-01-01 12:00:00 NaN NaN 2020-01-02 00:00:00 1.0 10.0 2020-01-02 12:00:00 2.0 12.0 2020-01-03 00:00:00 3.0 14.0 将索引负向移动三个周期 Id Age 2020-01-01 00:00:00 3.0 14.0 2020-01-01 12:00:00 4.0 11.0 2020-01-02 00:00:00 5.0 13.0 2020-01-02 12:00:00 NaN NaN 2020-01-03 00:00:00 NaN NaNSolutionTo ... 阅读更多
252 次查看
假设您有一个数据框,删除第一行重复行的结果是, Id Age 0 1 12 3 4 13 4 5 14 5 6 12 6 2 13 7 7 16 8 3 14 9 9 15 10 10 14SolutionTo solve this, we will follow the steps given below −定义一个数据框应用 drop_duplicates 函数到 Id 和 Age 列,并将 keep 的初始值设置为 ‘last’。df.drop_duplicates(subset=['Id', 'Age'], keep='last')将结果存储到同一个数据框中并打印它Example让我们看看下面的实现以更好地理解 −import pandas ... 阅读更多
295 次查看
假设您有一个数据框,计算分组数据协方差及其对应列的结果如下,分组数据协方差为: mark1 mark2 subjects maths mark1 25.0 12.500000 mark2 12.5 108.333333 science mark1 28.0 50.000000 mark2 50.0 233.333333 两列之间的分组数据协方差: subjects maths 12.5 science 50.0 dtype: float64SolutionTo solve this, we will follow the steps given below −定义一个数据框应用 groupby 函数到数据框 subjects ... 阅读更多
392 次查看
我们可以使用 melt()、stack()、unstack() 和 pivot() 函数重塑数据框。Solution 1定义一个数据框。应用 melt() 函数将宽数据框的列转换为行。它定义如下, df.melt()Example让我们看看下面的代码以更好地理解 −import pandas as pd df = pd.DataFrame({'Id':[1, 2, 3], 'Age':[13, 14, 13], 'Mark':[80, 90, 85]}) print("Dataframe is:", df) print(df.melt())OutputDataframe is: Id Age Mark 0 1 13 80 1 2 14 90 2 3 13 85 variable value 0 Id 1 1 Id 2 2 Id 3 3 Age 13 4 ... 阅读更多
254 次查看
假设您有一个包含时间序列数据的数据框,截断后的数据结果如下,截断前: Id time_series 0 1 2020-01-05 1 2 2020-01-12 2 3 2020-01-19 3 4 2020-01-26 4 5 2020-02-02 5 6 2020-02-09 6 7 2020-02-16 7 8 2020-02-23 8 9 2020-03-01 9 10 2020-03-08 截断后: Id time_series 1 2 2020-01-12SolutionTo solve this, we will follow the steps given below −定义一个数据框。在 start=’01/01/2020’,periods = 10 内创建 date_range 函数,并将其 freq 设置为 ‘W’。它将从给定的开始日期生成十个日期到下一个每周开始日期,并将其存储为 df[‘time_series’]。df['time_series'] ... 阅读更多
231 次查看
假设您有一个序列,滞后 2 的自相关结果如下,序列为: 0 2.0 1 10.0 2 3.0 3 4.0 4 9.0 5 10.0 6 2.0 7 NaN 8 3.0 dtype: float64 序列相关性: -0.4711538461538461 滞后序列相关性: -0.2933396642805515SolutionTo solve this, we will follow the steps given below −定义一个序列使用以下方法查找序列自相关,series.autocorr()计算滞后=2 的自相关,如下所示,series.autocorr(lag=2)Example让我们看看下面的代码以更好地理解,import pandas as pd import numpy as np series = ... 阅读更多
156 次查看
假设您有一个数据框,将数据框导出到 pickle 文件并从文件中读取内容的结果如下,导出到 pickle 文件: 从 pickle 文件读取内容: Fruits City 0 Apple Shimla 1 Orange Sydney 2 Mango Lucknow 3 Kiwi WellingtonSolutionTo solve this, we will follow the steps given below −定义一个数据框。将数据框导出为 pickle 格式,并将其命名为 ‘pandas.pickle’,df.to_pickle('pandas.pickle')从 ‘pandas.pickle’ 文件中读取内容并将其存储为 result,result = pd.read_pickle('pandas.pickle')Example让我们看看下面的实现以更好地理解,import pandas as pd df = pd.DataFrame({'Fruits': ... 阅读更多
980 次查看
假设您在文件中存储了以下示例 json 数据,如 pandas_sample.json{ "employee": { "name": "emp1", "salary": 50000, "age": 31 } }转换为 csv 后,结果如下, , employee age, 31 name, emp1 salary, 50000SolutionTo solve this, we will follow the steps given below −创建 pandas_sample.json 文件并存储 JSON 数据。从文件中读取 json 数据并将其存储为 data。data = pd.read_json('pandas_sample.json')将数据转换为 dataframedf = pd.DataFrame(data)Apple df.to_csv 函数将数据转换为 csv 文件格式,df.to_csv('pandas_json.csv')Example让我们看看下面的实现 ... 阅读更多
140 次查看
假设您有时间序列,最大月底频率的结果如下,DataFrame 为: Id time_series 0 1 2020-01-05 1 2 2020-01-12 2 3 2020-01-19 3 4 2020-01-26 4 5 2020-02-02 5 6 2020-02-09 6 7 2020-02-16 7 8 2020-02-23 8 9 2020-03-01 9 10 2020-03-08 最大月底频率: Id time_series time_series 2020-01-31 4 2020-01-26 2020-02-29 8 2020-02-23 2020-03-31 10 2020-03-08SolutionTo solve this, we will follow the steps given below −定义一个包含一列的数据框,d = {'Id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} ... 阅读更多
1K+ 次查看
假设我们已经保存了 pandas.csv 文件,并希望将其导出为 Html 格式。解决方案为了解决这个问题,我们将按照以下步骤操作:使用 read_csv 方法读取 csv 文件,如下所示:df = pd.read_csv('pandas.csv')使用文件对象以写入模式创建新的文件 pandas.html,f = open('pandas.html', 'w')声明 result 变量,用于将数据框转换为 html 文件格式,result = df.to_html()使用文件对象写入 result 中的所有数据。最后关闭文件对象,f.write(result) f.close()示例让我们看看下面的实现,以便更好地理解:import pandas as pd df = pd.read_csv('pandas.csv') print(df) f = open('pandas.html', 'w') result ... 阅读更多
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP