简体   繁体   English

如何将行变成列?

[英]How can I turn rows into columns?

I have a pandas Data Frame that looks like this:我有一个 pandas 数据框,如下所示:

+------+------------+-----------+
| FIPS |    Date    | Confirmed |
+------+------------+-----------+
|   66 | 04/02/2020 |        82 |
|   66 | 04/03/2020 |        84 |
|   66 | 04/04/2020 |        93 |
+------+------------+-----------+

I want to turn this into one row with the date appended to the confirmed column:我想把它变成一行,并将日期附加到已确认的列:

+------+--------------------+--------------------+--------------------+
| FIPS | Confirmed_20200402 | Confirmed_20200402 | Confirmed_20200402 |
+------+--------------------+--------------------+--------------------+
| 66   | 82                 | 84                 | 93                 |
+------+--------------------+--------------------+--------------------+

I tried to use pivot_table, however, that giving me a multi-index table which isn't what I want.然而,我尝试使用 pivot_table,它给了我一个多索引表,这不是我想要的。

How can I go about getting my desired output?我怎样才能 go 获得我想要的 output?

You can do:你可以做:

df['Date']= pd.to_datetime(df['Date'], dayfirst=True).dt.date

dd = pd.pivot_table(df, index='FIPS', columns='Date', values='Confirmed')

# fix column names
dd.columns.name = None
dd.columns = dd.add_prefix('Confirmed_').columns.str.replace('\-','')

dd = dd.reset_index()
print(dd)

   FIPS  Confirmed_20200204  Confirmed_20200304  Confirmed_20200404
0    66                  82                  84                  93

Use unstack :使用unstack

df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y%m%d')
df = df.set_index(['FIPS', 'Date']).unstack()
df.columns = df.columns.get_level_values(0) + '_' + df.columns.get_level_values(1)

We can achieve your output with pivot and flattening the multiIndex columns.我们可以用 pivot 实现您的 output 并展平 multiIndex 列。

df = pd.DataFrame({'FIPS':[66]*3
                  ,'Date':pd.date_range('04/02/2020', periods=3, freq='D')
                  ,'Confirmed':[82,84,93]})

df_p = df.pivot('FIPS','Date')
df_p.columns = [f'{i}_{j.strftime("%Y%m%d")}' for i, j in df_p.columns]
df_p

Output: Output:

      Confirmed_20200402  Confirmed_20200403  Confirmed_20200404
FIPS                                                            
66                    82                  84                  93

Another way using pivot and defining values parameter:另一种使用 pivot 并定义values参数的方法:

df_p = df.assign(Date=df['Date'].dt.strftime('%Y%m%d')).pivot('FIPS', 'Date', 'Confirmed').add_prefix('Confirmed_')
df_p

Output: Output:

Date  Confirmed_20200402  Confirmed_20200403  Confirmed_20200404
FIPS                                                            
66                    82                  84                  93

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM