[英]How to read a csv row wise and convert to a dataframe column using pandas?
[英]How to read the data in Pandas's dataframe row wise and plot the values as a timeseries if column represents months and index is years?
我有一個數據框,它以月份為一列,第一列代表年份。 我想 plot 這個數據幀的時間序列,即讀取每一行並繪制一個時間序列。 我在下面提供了我的數據框的一小部分。 請讓我知道執行此任務的任何方法。
年份 1 月 2 月 3 月 4 月 5 月 6 月 7 月 8 月 9 月 10 月 11 月 12 月
0 1870 -0.02 -0.02 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01
1 1871 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01 0.00 0.00 0.00
等等....
我假設您將數據存儲在 pandas DataFrame 中,格式如下(每行代表一年):
df = pd.DataFrame(np.array([[1870,0.02,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01],
[1871,0.02,0.02,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01]]),
columns = ["YEAR", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"])
以這種方式輸出 df :
YEAR JAN FEB MAR APR MAY ... JUL AUG SEP OCT NOV DEC
0 1870.0 0.02 0.02 0.01 0.01 0.01 ... 0.01 0.01 0.01 0.01 0.01 0.01
1 1871.0 0.02 0.02 0.01 0.01 0.01 ... 0.01 0.01 0.01 0.01 0.01 0.01
[2 rows x 13 columns]
這只是一個包含重復行條目的兩年樣本。
您需要的是如下所示:
import matplotlib.pyplot as plt
cols = np.array(df.columns)[1:]
rows_size= df.shape[0]
x = np.empty((1, 0), str)
y = np.empty((1, 0), float)
for i in range (rows_size):
x = np.append(x, str(int(df.iloc[i, 0]))+ "-" + cols.reshape(1,12) , axis = 1)
y = np.append(y, np.array(df.iloc[i, 1:]).reshape(1,12), axis = 1)
x = x.reshape(-1)
y = y.reshape(-1)
plt.plot(x, y)
plt.xticks(x,x, rotation ='vertical')
plt.subplots_adjust(bottom = 0.2)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.