我将数据从 csv 加载到 Python Pandas 并尝试使列成行(具有重复日期)

I load a data from csv to Python Pandas and try to make the columns to row (with duplicates dates)

:

我从这里获取确认数据案例: https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv

我使用 Pandas Dataframe 在 python 中加载数据。

我的问题是:我试图将日期的列设为行,将“Country/Region”列设为列。

url_confirmed = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'

df = pd.read_csv(url_confirmed)

df = df.drop(columns=['Province/State','Lat','Long'],axis=1)

df_piv = pd.melt(df,id_vars=['Country/Region'],var_name='Date',value_name="Value")

我到了这里,真的不知道如何进行

我的最终数据框应该是这样的:

   Date    Afghanistan    Albania   and so on
0  1/22/20    0              val  
1  1/23/20    300            val
3  1/24/20   4023            val
6  1/25/20    300            val
7  1/26/20   2000            val
8    ..       ..
.
.

**非常感谢**

我认为重命名列的简单转置应该可以做到:

url_confirmed = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'

df = pd.read_csv(url_confirmed)

df = df.drop(columns=['Province/State','Lat','Long'],axis=1)

df = df.T.reset_index() # Transpose and reset index
df.columns = df.iloc[0] # Set first row as header
df = df[1:]
df.rename(columns = {'Country/Region' : 'Date'}, inplace=True)