[英]How to concatenate dataframes in Pandas
I have weather data about Min and Max temperatures and Precipitation of various weather stations over a period of 1985-2014. 我有1985-2014年间有关最低和最高温度的气象数据以及各个气象站的降水。 I have found the average Min and Max temperatures for each year using GroupBy in Pandas.
我已经在熊猫中使用GroupBy找到了每年的平均最低和最高温度。
data1= data
#Replaceing missing values represented by -9999 with 0
df2=data1.replace(to_replace=-9999,value=0)
#performing groupby over the year part of the string given in Date
df3=data1.groupby(df2.Date.str[0:4])
tmp=df3['MaxTemp'].mean().to_frame()
Date MaxTemp 1985 153.347945 1986 126.963370 .... ......
fileName=filePath.split('\\')[-1]
#filename is USC00110072.txt
fname=pd.DataFrame([fileName]*len(tmp.index))
fname.columns=['File']
# mtemp=pd.concat([])
fname.index=[i for i in range(1985,2015)]
fname
Now I want to concatenate tmp and fname dataframes to give me data as such: 现在,我想连接tmp和fname数据帧,以便为我提供这样的数据:
FileName Year MaxTemp USC00110072.txt 1985 153.347945205 USC00110072.txt 1986 126.963369963 .... ... ......
To do this I used pandas.concat([fname,tmp], axis=1)
but I am getting following output: 为此,我使用了
pandas.concat([fname,tmp], axis=1)
但我得到以下输出:
File MaxTemp
1985 USC00110072.txt NaN
1986 USC00110072.txt NaN
1987 USC00110072.txt NaN
.... ........ ...
1985 NaN 153.347945
1986 NaN 126.963370
1987 NaN 177.602740
Please suggest what changes should I make in the concat function so that I get the desired output. 请建议我应该在concat函数中进行哪些更改,以便获得所需的输出。
Problem is in first dataframes years are strings, in second integers. 问题在于第一个数据帧中的年份是字符串,第二个整数。
So need convert string
s to integer
s by astype
: 因此需要通过
astype
将string
s转换为integer
s:
tmp = (data.replace(to_replace=-9999,value=0)
.groupby(data.Date.str[0:4].astype(int))
.mean()
.to_frame())
Also solution should be simplify by assign
for append new column with same values: 还应通过
assign
具有相同值的新列来简化解决方案:
df = (data.replace(to_replace=-9999,value=0)
.groupby(data.Date.str[0:4].astype(int))
.mean()
.to_frame()
.assign(File = fileName))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.