我想知道如何按月對資料進行分組,以便我可以按月查看資料。我該怎么做?
例如,將 1 月記錄在他們自己的資料框中的所有資料分配給 january 以進行分析等。
這是我當前的資料框:
WC_Humidity[%] WC_Htgsetp[C] WC_Clgsetp[C] Date Time
0 55.553640 18 26 2005-01-01 00:10
1 54.204342 18 26 2005-01-01 00:20
2 51.896272 18 26 2005-01-01 00:30
3 49.007770 18 26 2005-01-01 00:40
4 45.825810 18 26 2005-01-01 00:50
非常感謝幫助。
uj5u.com熱心網友回復:
嘗試這個:
df1['Date'].to_numpy().astype('datetime64[M]')
uj5u.com熱心網友回復:
您可以使用以下代碼轉換您的列。
df1['Date'] = pd.to_datetime(df["Date"].dt.strftime('%d-%m-%Y'))
您可以參考官方檔案了解為什么 dayfirst 不起作用。 https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html
uj5u.com熱心網友回復:
如果該列的格式為 2021-01-29, 30-12-2024,那么在上述行之前應該處理它并相應地決議它。
pd.to_datetime(df1['Date'])
現在您可以使用此代碼將日期列轉換為您想要的方式。
df1['Date'] = df['Date1'].dt.strftime('%d/%m/%Y')
這應該讓你得到你想要的。
uj5u.com熱心網友回復:
如果你有這樣的字串,2005-01-01那么你可以得到
df['year-month'] = df['Date'].str[:7]
稍后你可以使用
df.groupby('year-month')
最少的作業代碼。
我更改了日期以在資料中包含不同的月份。
我io只用來模擬記憶體中的檔案。
text = '''WC_Humidity[%],WC_Htgsetp[C],WC_Clgsetp[C],Date,Time
55.553640,18,26,2005-01-01,00:10
54.204342,18,26,2005-01-01,00:20
51.896272,18,26,2005-02-01,00:30
49.007770,18,26,2005-02-01,00:40
45.825810,18,26,2005-03-01,00:50
'''
import pandas as pd
import io
df = pd.read_csv(io.StringIO(text))
df['year-month'] = df['Date'].str[:7]
print(df)
for value, group in df.groupby('year-month'):
print()
print('---', value, '---')
print(group)
print()
print('average WC_Humidity[%]:', group['WC_Humidity[%]'].mean())
結果:
WC_Humidity[%] WC_Htgsetp[C] WC_Clgsetp[C] Date Time year-month
0 55.553640 18 26 2005-01-01 00:10 2005-01
1 54.204342 18 26 2005-01-01 00:20 2005-01
2 51.896272 18 26 2005-02-01 00:30 2005-02
3 49.007770 18 26 2005-02-01 00:40 2005-02
4 45.825810 18 26 2005-03-01 00:50 2005-03
--- 2005-01 ---
WC_Humidity[%] WC_Htgsetp[C] WC_Clgsetp[C] Date Time year-month
0 55.553640 18 26 2005-01-01 00:10 2005-01
1 54.204342 18 26 2005-01-01 00:20 2005-01
average WC_Humidity[%]: 54.878991
--- 2005-02 ---
WC_Humidity[%] WC_Htgsetp[C] WC_Clgsetp[C] Date Time year-month
2 51.896272 18 26 2005-02-01 00:30 2005-02
3 49.007770 18 26 2005-02-01 00:40 2005-02
average WC_Humidity[%]: 50.452021
--- 2005-03 ---
WC_Humidity[%] WC_Htgsetp[C] WC_Clgsetp[C] Date Time year-month
4 45.82581 18 26 2005-03-01 00:50 2005-03
average WC_Humidity[%]: 45.82581
如果你有物件,datetime那么你可以做
df['year-month'] = df['Date'].dt.strftime('%Y-%m')
休息是一樣的
text = '''WC_Humidity[%],WC_Htgsetp[C],WC_Clgsetp[C],Date,Time
55.553640,18,26,2005-01-01,00:10
54.204342,18,26,2005-01-01,00:20
51.896272,18,26,2005-02-01,00:30
49.007770,18,26,2005-02-01,00:40
45.825810,18,26,2005-03-01,00:50
'''
import pandas as pd
import io
df = pd.read_csv(io.StringIO(text))
# create datetime objects
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
df['year-month'] = df['Date'].dt.strftime('%Y-%m')
print(df)
for value, group in df.groupby('year-month'):
print()
print('---', value, '---')
print(group)
print()
print('average WC_Humidity[%]:', group['WC_Humidity[%]'].mean())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/349182.html
