如何基于for循环中的数据构造新的数据帧?

2024-05-19 12:34:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集(datacomplete2),其中我有每个国家两年的数据。我想计算每个国家这几年的差异(生命价值、健康和生命GDP),并用结果创建一个新的数据框架

守则:

for i in datacomplete2['Country'].unique():
    life.append(datacomplete2.loc[(datacomplete2['Country']==i)&(datacomplete2['Year']==2016), 'life'] - datacomplete2.loc[(datacomplete2['Country']==i)&(datacomplete2['Year']==2000), 'life'])
    health.append(datacomplete2.loc[(datacomplete2['Country']==i)&(datacomplete2['Year']==2016), 'health'] - datacomplete2.loc[(datacomplete2['Country']==i)&(datacomplete2['Year']==2000), 'health'])
    lifegdp.append(datacomplete2.loc[(datacomplete2['Country']==i)&(datacomplete2['Year']==2016), 'lifegdp'] - datacomplete2.loc[(datacomplete2['Country']==i)&(datacomplete2['Year']==2000), 'lifegdp'])

newData = pd.DataFrame([life, health, lifegdp, datacomplete2['Country'].unique()], columns = ['life', 'health', 'lifegdp', 'country'])

newData

我认为用于计算的for循环是正确的,问题在于创建新的数据帧。当我试图运行代码时,我得到一条错误消息:传递了4列,传递的数据有210列

我有210个国家,所以我假设它会以某种方式将这些值放到列中

这里还有一个链接,可以看到我正在使用的数据:https://i.imgur.com/jbGFPpk.png

以文本形式显示的数据如下所示:

    Country Code    Year    life    health  lifegdp

0   Algeria DZA 2000    70.292000   3.489033    20.146558

1   Algeria DZA 2016    76.078000   6.603844    11.520259

2   Angola  AGO 2000    47.113000   1.908599    24.684593

3   Angola  AGO 2016    61.547000   2.713149    22.684710

4   Antigua and Barbuda ATG 2000    73.541000   4.480701    16.412834

... ... ... ... ... ... ...

415 Vietnam VNM 2016    76.253000   5.659194    13.474181

416 World   OWID_WRL    2000    67.684998   8.617628    7.854249

417 World   OWID_WRL    2016    72.035337   9.978453    7.219088

418 Zambia  ZMB 2000    44.702000   7.152371    6.249955

419 Zambia  ZMB 2016    61.874000   4.477207    13.819775

需要快速帮助

我两周前就开始编写代码了,所以我对这方面非常熟悉


Tags: 数据代码for国家yearcountrylocunique
2条回答

你可以这样做

country_list = df.Country.unique().tolist()
df.drop(columns = ['Code'])

df_2016 = df.loc[(df['Country'].isin(country_list))&(df['Year']==2016)].reset_index()
df_2000 = df.loc[(df['Country'].isin(country_list))&(df['Year']==2000)].reset_index()
df_2016.drop(columns=['Year'])
df_2000.drop(columns=['Year'])
df_2016.set_index('Country').subtract(df_2000.set_index('Country'), fill_value=0)

Anurag Reddy的答案是一个很好的简明解决方案,如果你提前知道日期的话。为了给出一个更一般的备选答案,这个问题是pandas.DataFrame.diff的一个很好的示例用例

注意,您实际上不需要对示例数据中的数据进行排序,但我在下面包含了一行sort_values()来说明未排序的数据帧

import pandas as pd

# Read the raw datafile in
df = pd.read_csv("example.csv")

# Sort the data if required
df.sort_values(by=["Country"], inplace=True)

# Remove columns where you don't need the difference
new_df = df.drop(["Code", "Year"], axis=1)

# Group the data by country, take the difference between the rows, remove NaN rows, and reset the index to sequential integers
new_df = new_df.groupby(["Country"], as_index=False).diff().dropna().reset_index(drop=True)

# Add back the country names and codes as columns in the new DataFrame
new_df.insert(loc=0, column="Country", value=df["Country"].unique())
new_df.insert(loc=1, column="Code", value=df["Code"].unique())

相关问题 更多 >