Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于现有df获取新的数据帧,并将值作为列_Python_Pandas - Fatal编程技术网

Python 基于现有df获取新的数据帧,并将值作为列

Python 基于现有df获取新的数据帧,并将值作为列,python,pandas,Python,Pandas,我得到了什么? for date in dates: df_result[date] = df.loc[df['date'] == date, 'value'] 我已获得以下df和日期列表: df id value date 0 1 1 2020-01-01 1 1 2 2020-02-01 2 1 3 2020-03-01 3 2 3 2020-01-01 4 2 4 2020-02

我得到了什么?

for date in dates:
    df_result[date] = df.loc[df['date'] == date, 'value']
我已获得以下df和日期列表:

df
   id  value        date
0   1      1  2020-01-01
1   1      2  2020-02-01
2   1      3  2020-03-01
3   2      3  2020-01-01
4   2      4  2020-02-01
5   2      5  2020-03-01
6   3      6  2020-01-01
7   3      7  2020-02-01
8   3      8  2020-03-01

dates = ['2020-01-01','2020-02-01','2020-03-01']
我想要什么?

for date in dates:
    df_result[date] = df.loc[df['date'] == date, 'value']
我想要一个具有不同id的新df,并为每个日期创建一个具有以下值的新列:

   id  2020-01-01  2020-02-01  2020-03-01
0   1         1         2         3
1   2         4         5         6
2   3         7         8         9

我试过什么?

for date in dates:
    df_result[date] = df.loc[df['date'] == date, 'value']
这就是我得到的:


   id  2020-01-01  2020-02-01  2020-03-01
0   1         1.0         NaN         NaN
1   2         NaN         2.0         NaN
2   3         NaN         NaN         3.0

正如您所看到的,这是错误的,有什么解决方案吗?

如果日期是字符串,请使用列表作为筛选依据,然后:

上次将
id
转换为列:

df = df.reset_index().rename_axis(None, axis=1)
print (df)
   id  2020-01-01  2020-02-01  2020-03-01
0   1           1           2           3
1   2           3           4           5
2   3           6           7           8
带有datetimes的解决方案-还需要转换列表:

df['date'] = pd.to_datetime(df['date'])

dates = ['2020-01-01','2020-02-01','2020-03-01']

df = df[df['date'].isin(pd.to_datetime(dates))].pivot('id','date','value')
print (df)
date  2020-01-01  2020-02-01  2020-03-01
id                                      
1              1           2           3
2              3           4           5
3              6           7           8
上次将
id
转换为列时,添加了隐藏时间:

df = df.reset_index().rename_axis(None, axis=1)
print (df)
   id  2020-01-01 00:00:00  2020-02-01 00:00:00  2020-03-01 00:00:00
0   1                    1                    2                    3
1   2                    3                    4                    5
2   3                    6                    7                    8
要删除它们,可以将日期时间转换为字符串:

df = (df.rename(columns = lambda x: x.strftime('%Y-%m-%d'))
        .reset_index()
        .rename_axis(None, axis=1))
print (df)
   id  2020-01-01  2020-02-01  2020-03-01
0   1           1           2           3
1   2           3           4           5
2   3           6           7           8