美文网首页
[old]互为你我:Python 与 Stata 对比

[old]互为你我:Python 与 Stata 对比

作者: stata连享会 | 来源:发表于2019-04-19 14:59 被阅读0次

    来源: Comparison with Stata

    译者:郭李鹏 (山西晋城市财政局)

    Stata 连享会: 知乎 | 简书 | 码云

    连享会计量方法专题……

    编者按: 不同语言和软件有其独特功能。在研究工作中,我们往往会同时采用多种工具完成我们的研究任务。本文通过实例对比 Python 和 Stata 在语法上的差别,以便于各位能在二者之间自如切换。事实上,两边的用户都在努力促成彼此的融合,介绍 Python 与 Stata 结合的文章如下:

    对于使用 Stata 的用户,本文通过对比演示如何用 pandas 实现 Stata 的基本操作。

    如果你对 pandas 还不熟悉,你可以先通读一遍 10 分钟入门 pandas ,熟悉一下相关库。

    通常我们按照如下方式导入 pandas 和 numpy 库,在本文后续部分,我们将使用 pd 和 np 分别代表相关库。

    In [1]: import pandas as pd
    
    In [2]: import numpy as np
    

    请注意

    本文通过调用 df.head() 命令来显示 pandas 数据集 DataFrame 的前 N ( 默认 5 ) 行数据。 Stata 中对应命令为:

    list in 1/5
    

    1. 数据结构

    1.1 常用术语对照

    pandas Stata
    DataFrame data set
    column variable
    row observation
    groupby bysort
    NaN .

    1.2 DataFrame / Series

    pandas 中的 DataFrame 类似于 Stata 数据集——一个二维数据,每列可以是不同的值类型。正如本文将要展示,几乎任何可以对 Stata 的数据集应用的操作都可以在 pandas 中完成。

    Series 是表示 DataFrame 列的数据结构。 Stata 没有针对列的单独数据结构,但通常,使用 Series 类似于引用 Stata 数据集中的列。

    1.3 Index

    每个DataFrameSeries都有一个Index索引——数据 的标签。 Stata 没有一个完全类似的概念。在 Stata 中,除了可以用暗含索引_n 访问行外,数据基本没有行标签。

    在 pandas 中,即使没有指定索引,也可以使用默认整数索引(第一行= 0,第二行= 1,依次类推)。虽然使用单个索引或多个索引可以进行复杂的分析,并且是 pandas 的重要功能,但是为了与 Stata 比较,本文中将基本忽略行索引,只将 DataFrame 作为列的集合。获取更多有关使用索引的信息,请参阅索引文档。

    连享会计量方法专题……

    2. Data Input / Output

    2.1 创建 DataFrame

    Stata数据通过 input 语句赋值并指定列名。

    input x y
    1 2
    3 4
    5 6
    end
    

    创建 DataFrame 有很多,数据量较小时,最常用的方法是传入一个 Python 字典,其中字典每一个 key 对应一列,与 key 对应 value 是该列下所有数据。

    In [3]: df = pd.DataFrame({'x': [1, 3, 5], 'y': [2, 4, 6]})
    In [4]: df
    Out[4]: 
       x  y
    0  1  2
    1  3  4
    2  5  6
    

    2.2 读取外部数据

    与 Stata 一样,pandas 可以从许多格式文件中读取数据。可以通过链接获取 pandas 测试数据集 tips ,我们将在下面的许多示例中使用到这份数据。

    Stata 通过 import delimited 将 csv 数据文件读入内存中。如果 tips.csv 文件在当前工作目录中,我们可以按照以下方式导入。

    import delimited tips.csv
    

    pandas 通过 read_csv() 方法读取 csv 文件,与 Stata 工作原理类似。此外,pandas 可以通过 url 自动下载数据集。

    In [5]: url = ('https://raw.github.com/pandas-dev'
    ...:       '/pandas/master/pandas/tests/data/tips.csv')
    ...: 
    
    In [6]: tips = pd.read_csv(url)
    
    In [7]: tips.head()
    Out[7]: 
      total_bill   tip     sex  smoker  day    time  size
    0       16.99  1.01  Female     No  Sun  Dinner     2
    1       10.34  1.66    Male     No  Sun  Dinner     3
    2       21.01  3.50    Male     No  Sun  Dinner     3
    3       23.68  3.31    Male     No  Sun  Dinner     2
    4       24.59  3.61  Female     No  Sun  Dinner     4
    

    import delimited 一样,read_csv() 可以使用更多参数来导入数据。例如,如果数据是用 tab 分隔的,没有列名,并且存在于当前工作目录中, panda 命令将是:

    tips = pd.read_csv('tips.csv', sep='\t', header=None)
    
    # alternatively, read_table is an alias to read_csv with tab delimiter
    tips = pd.read_table('tips.csv', header=None)
    

    pandas 还可以使用 read_stata() 函数读取 .dta 格式的 Stata 数据。

    df = pd.read_stata('data.dta')
    

    除了 text/csv 和 Stata 文件之外,pandas 还支持许多种其他数据格式,如 Excel / SAS / HDF 5 / Parquet 和 SQL 数据库。这些都是通过 pd.read_* 相关命令读取的。有关详细信息,请参阅 IO 文档。

    2.3 导出数据

    在 Stata 中,与 import delimited 功能相反的函数是 export delimited

    export delimited tips2.csv
    

    类似地,在 pandas 中,read_csv 功能相反的函数是 DataFrame.to_csv()

    tips.to_csv('tips2.csv')
    

    pandas 还可以使用 DataFrame.to_stata() 方法导出到 Stata 格式文件。

    tips.to_stata('tips2.dta')
    

    3. 数据操作

    3.1 列操作

    在 Stata 中,可以使用 generatereplace 在数据某一列或新产生一列上运算任意数学公式。

    replace total_bill = total_bill - 2
    generate new_bill = total_bill / 2
    drop new_bill
    

    pandas 通过在 DataFrame 中 x 选定各个 Series 来进行类似的向量化操作。可以用同样的方法产生新的列。可以用 DataFrame.drop() 方法从 DataFrame 中删除列。

    In [8]: tips['total_bill'] = tips['total_bill'] - 2
    
    In [9]: tips['new_bill'] = tips['total_bill'] / 2
    
    In [10]: tips.head()
    Out[10]: 
    total_bill   tip     sex smoker  day    time  size  new_bill
    0       14.99  1.01  Female     No  Sun  Dinner     2     7.495
    1        8.34  1.66    Male     No  Sun  Dinner     3     4.170
    2       19.01  3.50    Male     No  Sun  Dinner     3     9.505
    3       21.68  3.31    Male     No  Sun  Dinner     2    10.840
    4       22.59  3.61  Female     No  Sun  Dinner     4    11.295
    
    In [11]: tips = tips.drop('new_bill', axis=1)
    

    3.2 条件过滤

    Stata 中使用 if 语句对一个或多个列的数据进行筛选。

    list if total_bill > 10
    

    DataFrames 有多种过滤方式,最直观的是使用 boolean indexing

    In [12]: tips[tips['total_bill'] > 10].head()
    Out[12]: 
      total_bill   tip     sex  smoker  day    time  size
    0       14.99  1.01  Female     No  Sun  Dinner     2
    2       19.01  3.50    Male     No  Sun  Dinner     3
    3       21.68  3.31    Male     No  Sun  Dinner     2
    4       22.59  3.61  Female     No  Sun  Dinner     4
    5       23.29  4.71    Male     No  Sun  Dinner     4
    

    3.3 If 条件语句

    在 Stata 中,可以使用 if 语句来创建新列。

    generate bucket = "low" if total_bill < 10
    replace bucket = "high" if total_bill >= 10
    

    在 pandas 中也可以使用 numpywhere 方法来完成相同的操作。

    In [13]: tips['bucket'] = np.where(tips['total_bill'] < 10, 'low', 'high')
    
    In [14]: tips.head()
    Out[14]: 
       total_bill   tip     sex  smoker  day    time  size bucket
    0       14.99  1.01  Female     No  Sun  Dinner     2   high
    1        8.34  1.66    Male     No  Sun  Dinner     3    low
    2       19.01  3.50    Male     No  Sun  Dinner     3   high
    3       21.68  3.31    Male     No  Sun  Dinner     2   high
    4       22.59  3.61  Female     No  Sun  Dinner     4   high
    

    3.4 日期函数

    Stata提供了一系列函数来处理数据类型为 date/datetime 的列。

    generate date1 = mdy(1, 15, 2013)
    generate date2 = date("Feb152015", "MDY")
    
    generate date1_year = year(date1)
    generate date2_month = month(date2)
    
    * shift date to beginning of next month
    generate date1_next = mdy(month(date1) + 1, 1, year(date1)) if month(date1) != 12
    replace date1_next = mdy(1, 1, year(date1) + 1) if month(date1) == 12
    generate months_between = mofd(date2) - mofd(date1)
    
    list date1 date2 date1_year date2_month date1_next months_between
    

    pandas 中同样的操作如下所示。除了这些功能之外, pandas 还支持其他在无法在 Stata 使用的处理时间序列的功能 ( 例如时区处理和自定义偏移量 ) —— 有关更多内容,请参见 timeseries 文档

    In [15]: tips['date1'] = pd.Timestamp('2013-01-15')
    
    In [16]: tips['date2'] = pd.Timestamp('2015-02-15')
    
    In [17]: tips['date1_year'] = tips['date1'].dt.year
    
    In [18]: tips['date2_month'] = tips['date2'].dt.month
    
    In [19]: tips['date1_next'] = tips['date1'] + pd.offsets.MonthBegin()
    
    In [20]: tips['months_between'] = (tips['date2'].dt.to_period('M')
     ....:                          - tips['date1'].dt.to_period('M'))
     ....: 
    
    In [21]: tips[['date1', 'date2', 'date1_year', 'date2_month', 'date1_next',
     ....:      'months_between']].head()
     ....: 
    Out[21]: 
           date1      date2  date1_year  date2_month date1_next    months_between
    0 2013-01-15 2015-02-15        2013            2 2013-02-01  <25 * MonthEnds>
    1 2013-01-15 2015-02-15        2013            2 2013-02-01  <25 * MonthEnds>
    2 2013-01-15 2015-02-15        2013            2 2013-02-01  <25 * MonthEnds>
    3 2013-01-15 2015-02-15        2013            2 2013-02-01  <25 * MonthEnds>
    4 2013-01-15 2015-02-15        2013            2 2013-02-01  <25 * MonthEnds>
    

    3.5 列操作

    Stata 通过关键字对列进行选择、删除和重命名的操作。

    keep sex total_bill tip
    
    drop sex
    
    rename total_bill total_bill_2
    

    pandas 中同样的操作如下表示。注意,与Stata相反,这些操作没有在数据原来的位置运算。要使这些更改生效,需要将操作赋给一个变量。

    # keep
    In [22]: tips[['sex', 'total_bill', 'tip']].head()
    Out[22]: 
         sex  total_bill   tip
    0  Female       14.99  1.01
    1    Male        8.34  1.66
    2    Male       19.01  3.50
    3    Male       21.68  3.31
    4  Female       22.59  3.61
    
    # drop
    In [23]: tips.drop('sex', axis=1).head()
    Out[23]: 
      total_bill   tip  smoker  day    time  size
    0       14.99  1.01     No  Sun  Dinner     2
    1        8.34  1.66     No  Sun  Dinner     3
    2       19.01  3.50     No  Sun  Dinner     3
    3       21.68  3.31     No  Sun  Dinner     2
    4       22.59  3.61     No  Sun  Dinner     4
    
    # rename
    In [24]: tips.rename(columns={'total_bill': 'total_bill_2'}).head()
    Out[24]: 
      total_bill_2   tip     sex  smoker  day    time  size
    0         14.99  1.01  Female     No  Sun  Dinner     2
    1          8.34  1.66    Male     No  Sun  Dinner     3
    2         19.01  3.50    Male     No  Sun  Dinner     3
    3         21.68  3.31    Male     No  Sun  Dinner     2
    4         22.59  3.61  Female     No  Sun  Dinner     4
    

    3.6 数值排序

    Stata 中的排序是通过 sort 命令完成的。

    sort sex total_bill
    

    pandas 对象可以通过 DataFrame.sort_values() 方法对多列进行排序。

    In [25]: tips = tips.sort_values(['sex', 'total_bill'])
    
    In [26]: tips.head()
    Out[26]: 
         total_bill   tip     sex  smoker   day    time  size
    67         1.07  1.00  Female    Yes   Sat  Dinner     1
    92         3.75  1.00  Female    Yes   Fri  Dinner     2
    111        5.25  1.00  Female     No   Sat  Dinner     1
    145        6.35  1.50  Female     No  Thur   Lunch     2
    135        6.51  1.25  Female     No  Thur   Lunch     2
    

    连享会计量方法专题……

    4. 字符串处理

    4.1 字符串长度

    Stata 分别使用 strlen()ustrlen() 函数来确定 ASCII 和 Unicode 码的字符串长度。

    generate strlen_time = strlen(time)
    generate ustrlen_time = ustrlen(time)
    

    Python 使用 len 函数确定字符串的长度。在 python 3 中,所有的字符串都是 Unicode 码字符串。len 计算长度时包括字符串末尾的空格。可以使用 lenrstrip 来去除末尾空格影响。

    In [27]: tips['time'].str.len().head()
    Out[27]: 
    67     6
    92     6
    111    6
    145    5
    135    5
    Name: time, dtype: int64
    
    In [28]: tips['time'].str.rstrip().str.len().head()
    Out[28]: 
    67     6
    92     6
    111    6
    145    5
    135    5
    Name: time, dtype: int64  
    

    4.2 查找字符串的位置

    Stata 使用 strpos() 函数确定字符串中字符的位置。在第一个参数定义的字符串,并搜索第二个参数提供的子字符串第一个出现的位置。

    generate str_position = strpos(sex, "ale")
    

    Python 使用 find() 函数确定字符串中字符的位置。 find() 函数搜索子字符串出现的第一个位置,如果找到子字符串,函数返回它的位置;如果没有找到子字符串,函数将返回-1。注意, Python 索引是从零开始的。

    In [29]: tips['sex'].str.find("ale").head()
    Out[29]: 
    67     3
    92     3
    111    3
    145    3
    135    3
    Name: sex, dtype: int64
    

    4.3 按位置提取子字符串

    Stata 使用 substr() 函数根据字符串的位置从字符串中提取子字符串。

    generate short_sex = substr(sex, 1, 1)
    

    对于 pandas ,您可以使用 [] 符号从字符串中按位置提取子字符串。注意, Python 索引是从零开始的。

    In [30]: tips['sex'].str[0:1].head()
    Out[30]: 
    67     F
    92     F
    111    F
    145    F
    135    F
    Name: sex, dtype: object
    

    4.4 提取第 n 个单词

    Stata 中 word() 函数的作用是:返回字符串中的第n个单词。第一个参数是要分析的字符串,第二个参数指定要提取哪个位置单词。

    clear
    input str20 string
    "John Smith"
    "Jane Cook"
    end
    
    generate first_name = word(name, 1)
    generate last_name = word(name, -1)
    

    Python 使用正则表达式从字符串文本中提取子字符串。有很多功能强大的方法,但这里只展示一个简单的方法。

    In [31]: firstlast = pd.DataFrame({'string': ['John Smith', 'Jane Cook']});
    
    In [32]: firstlast['First_Name'] = firstlast['string'].str.split(" ", expand=True)[0]
    
    In [33]: firstlast['Last_Name'] = firstlast['string'].str.rsplit(" ", expand=True)[1]
    
    In [34]: firstlast
    Out[34]: 
           string First_Name Last_Name
    0  John Smith       John     Smith
    1   Jane Cook       Jane      Cook
    

    4.5 大小写转换

    Stata 分别使用strupper()strlower()strproper()ustrupper()ustrlower()ustrtitle() 函数转换 ASCII 和 Unicode 码字符串的大小写。

    clear
    input str20 string
    "John Smith"
    "Jane Cook"
    end
    
    generate upper = strupper(string)
    generate lower = strlower(string)
    generate title = strproper(string)
    list
    

    Python 中等效函数是 upper, lowertitle

    In [35]: firstlast = pd.DataFrame({'string': ['John Smith', 'Jane Cook']})
    
    In [36]: firstlast['upper'] = firstlast['string'].str.upper()
    
    In [37]: firstlast['lower'] = firstlast['string'].str.lower()
    
    In [38]: firstlast['title'] = firstlast['string'].str.title()
    
    In [39]: firstlast
    Out[39]: 
           string       upper       lower       title
    0  John Smith  JOHN SMITH  john smith  John Smith
    1   Jane Cook   JANE COOK   jane cook   Jane Cook
    

    5. 数据合并

    下面的表格将在合并示例中使用。

    In [40]: df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'],
     ....:                    'value': np.random.randn(4)})
     ....: 
    
    In [41]: df1
    Out[41]: 
      key     value
    0   A  0.469112
    1   B -0.282863
    2   C -1.509059
    3   D -1.135632
    
    In [42]: df2 = pd.DataFrame({'key': ['B', 'D', 'D', 'E'],
     ....:                    'value': np.random.randn(4)})
     ....: 
    
    In [43]: df2
    Out[43]: 
      key     value
    0   B  1.212112
    1   D -0.173215
    2   D  0.119209
    3   E -1.044236
    

    在 Stata 中,要执行合并命令,一个数据必须在内存中,另一个数据必须通过引用硬盘上的文件名称。相反,Python 必须将两个数据 DataFrames 同时放在内存中。

    默认情况下, Stata 通过外部连接数据,在合并之后,来自两个数据的所有观察值都留在内存中。通过使用创建的 _merge 变量中的不同值,可以只保留来自初始数据集、合并数据集或两者交集的观察值。

    * First create df2 and save to disk
    clear
    input str1 key
    B
    D
    D
    E
    end
    generate value = rnormal()
    save df2.dta
    
    * Now create df1 in memory
    clear
    input str1 key
    A
    B
    C
    D
    end
    generate value = rnormal()
    
    preserve
    
    * Left join
    merge 1:n key using df2.dta
    keep if _merge == 1
    
    * Right join
    restore, preserve
    merge 1:n key using df2.dta
    keep if _merge == 2
    
    * Inner join
    restore, preserve
    merge 1:n key using df2.dta
    keep if _merge == 3
    
    * Outer join
    restore
    merge 1:n key using df2.dta
    

    pandas DataFrames 有一个 DataFrame.merge() 方法,提供了类似的功能。不同的合并类型是通过设定 how 关键字实现的。

    In [44]: inner_join = df1.merge(df2, on=['key'], how='inner')
    
    In [45]: inner_join
    Out[45]: 
    key   value_x   value_y
    0   B -0.282863  1.212112
    1   D -1.135632 -0.173215
    2   D -1.135632  0.119209
    
    In [46]: left_join = df1.merge(df2, on=['key'], how='left')
    
    In [47]: left_join
    Out[47]: 
    key   value_x   value_y
    0   A  0.469112       NaN
    1   B -0.282863  1.212112
    2   C -1.509059       NaN
    3   D -1.135632 -0.173215
    4   D -1.135632  0.119209
    
    In [48]: right_join = df1.merge(df2, on=['key'], how='right')
    
    In [49]: right_join
    Out[49]: 
    key   value_x   value_y
    0   B -0.282863  1.212112
    1   D -1.135632 -0.173215
    2   D -1.135632  0.119209
    3   E       NaN -1.044236
    
    In [50]: outer_join = df1.merge(df2, on=['key'], how='outer')
    
    In [51]: outer_join
    Out[51]: 
    key   value_x   value_y
    0   A  0.469112       NaN
    1   B -0.282863  1.212112
    2   C -1.509059       NaN
    3   D -1.135632 -0.173215
    4   D -1.135632  0.119209
    5   E       NaN -1.044236
    

    6. 缺失值

    与 Stata 一样,在 Pandas 中使用浮点值 NaN (不是数字)表示数组中的缺失值。许多含义是相同的,例如,缺失值可以进行数值运算操作,默认情况下汇总运算将忽略这些缺失值。

    In [52]: outer_join
    Out[52]: 
      key   value_x   value_y
    0   A  0.469112       NaN
    1   B -0.282863  1.212112
    2   C -1.509059       NaN
    3   D -1.135632 -0.173215
    4   D -1.135632  0.119209
    5   E       NaN -1.044236
    
    In [53]: outer_join['value_x'] + outer_join['value_y']
    Out[53]: 
    0         NaN
    1    0.929249
    2         NaN
    3   -1.308847
    4   -1.016424
    5         NaN
    dtype: float64
    
    In [54]: outer_join['value_x'].sum()
    Out[54]: -3.5940742896293765
    

    与 Stata 不同之处在于,不能使用 pandas 缺失值的标记值进行比较。例如,在 Stata 中,可以使用标记值来过滤丢失数据。

    * Keep missing values
    list if value_x == .
    * Keep non-missing values
    list if value_x != .
    

    pandas 中无法使用这中方式,需要使用 pd.isna()pd.notna() 函数进行比较。

    In [55]: outer_join[pd.isna(outer_join['value_x'])]
    Out[55]: 
     key  value_x   value_y
    5   E      NaN -1.044236
    
    In [56]: outer_join[pd.notna(outer_join['value_x'])]
    Out[56]: 
     key   value_x   value_y
    0   A  0.469112       NaN
    1   B -0.282863  1.212112
    2   C -1.509059       NaN
    3   D -1.135632 -0.173215
    4   D -1.135632  0.119209
    

    pandas 还提供了其他处理缺失值的方法——一些在 Stata 中很难操作。例如,有一些方法可以删除所有有缺失值的行,可以用指定的值(比如平均值,或者前行值)替换缺失值。获取更多信息,请查阅缺失值文档

    # Drop rows with any missing value
    In [57]: outer_join.dropna()
    Out[57]: 
    key   value_x   value_y
    1   B -0.282863  1.212112
    3   D -1.135632 -0.173215
    4   D -1.135632  0.119209
    
    # Fill forwards
    In [58]: outer_join.fillna(method='ffill')
    Out[58]: 
    key   value_x   value_y
    0   A  0.469112       NaN
    1   B -0.282863  1.212112
    2   C -1.509059  1.212112
    3   D -1.135632 -0.173215
    4   D -1.135632  0.119209
    5   E -1.135632 -1.044236
    
    # Impute missing values with the mean
    In [59]: outer_join['value_x'].fillna(outer_join['value_x'].mean())
    Out[59]: 
    0    0.469112
    1   -0.282863
    2   -1.509059
    3   -1.135632
    4   -1.135632
    5   -0.718815
    Name: value_x, dtype: float64
    

    7. 分组运算

    7.1 分组求和

    Stata的 collapse 函数可按一个或多个关键变量分组,并计算数值列上的和。

    collapse (sum) total_bill tip, by(sex smoker)
    

    pandas 通过 groupby 方法,可以实现类似的求和。

    In [60]: tips_summed = tips.groupby(['sex', 'smoker'])['total_bill', 'tip'].sum()
    
    In [61]: tips_summed.head()
    Out[61]: 
                  total_bill     tip
    sex    smoker                    
    Female No          869.68  149.77
          Yes         527.27   96.74
    Male   No         1725.75  302.00
          Yes        1217.07  183.07
    

    7.2 分组变换

    在 Stata 中,对原始数据分组运算,通常会使用 bysortegen() 命令。例如,按吸烟变量分组后,每个观察值减去的组内平均值。

    bysort sex smoker: egen group_bill = mean(total_bill)
    generate adj_total_bill = total_bill - group_bill
    

    panda 使用 groubpy 方法提供了一个 分组变换 功能,可以这些类型的操作在一个操作中简洁地表达出来。

    In [62]: gb = tips.groupby('smoker')['total_bill']
    
    In [63]: tips['adj_total_bill'] = tips['total_bill'] - gb.transform('mean')
    
    In [64]: tips.head()
    Out[64]: 
         total_bill   tip     sex smoker   day    time  size  adj_total_bill
    67         1.07  1.00  Female    Yes   Sat  Dinner     1      -17.686344
    92         3.75  1.00  Female    Yes   Fri  Dinner     2      -15.006344
    111        5.25  1.00  Female     No   Sat  Dinner     1      -11.938278
    145        6.35  1.50  Female     No  Thur   Lunch     2      -10.838278
    135        6.51  1.25  Female     No  Thur   Lunch     2      -10.678278
    

    7.3 分组处理

    除了汇总分析之外,pandas 的 groupby 函数可以完成 Stata 中 bysort 其他大多数的处理功能。例如,按性别、吸烟者变量分组排序列后列出第一个观察结果。

    bysort sex smoker: list if _n == 1
    

    pandas 中,写法如下:

    In [65]: tips.groupby(['sex', 'smoker']).first()
    Out[65]: 
                   total_bill   tip   day    time  size  adj_total_bill
    sex    smoker 
    Female No            5.25  1.00   Sat  Dinner     1      -11.938278
           Yes           1.07  1.00   Sat  Dinner     1      -17.686344
    Male   No            5.51  2.00  Thur   Lunch     2      -11.678278
           Yes           5.25  5.15   Sun  Dinner     2      -13.506344
    

    8. 其他

    8.1 硬盘和内存

    pandas 和 Stata 都只在内存中运行。这意味着可以在 pandas 中加载的数据大小受计算机内存限制。如果需要处理外部数据,可以使用 dask.dataframe库,可以对硬盘上的 DataFrame 数据实现部分 pandas 功能。

    8.2 Python 与 Stata 结合的相关介绍

    关于我们

    • 「Stata 连享会」 由中山大学连玉君老师团队创办,定期分享实证分析经验, 公众号:StataChina
    • 公众号推文同步发布于 CSDN简书知乎Stata专栏。可在百度中搜索关键词 「Stata连享会」查看往期推文。
    • 点击推文底部【阅读原文】可以查看推文中的链接并下载相关资料。
    • 欢迎赐稿: 欢迎赐稿。录用稿件达 三篇 以上,即可 免费 获得一期 Stata 现场培训资格。
    • E-mail: StataChina@163.com
    • 往期推文:计量专题 || 精品课程 || 简书推文 || 公众号合集

    点击查看完整推文列表

    欢迎加入Stata连享会(公众号: StataChina)

    相关文章

      网友评论

          本文标题:[old]互为你我:Python 与 Stata 对比

          本文链接:https://www.haomeiwen.com/subject/bbwqgqtx.html