2 回答
TA贡献1798条经验 获得超3个赞
对于 Python:(
一)和(二):
for i in df.groupby(["Region", "country"])[["Region", "country"]].apply(lambda x: list(np.unique(x))):
df.groupby(["Region", "country"]).get_group((i[1], i[0])).to_csv(f"{i[1]}_{i[0]}.csv")
(三)、(四):
import glob
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=2, ncols=2)
for ax, file in zip(axs.flatten(), glob.glob("./*csv")):
df_temp = pd.read_csv(file)
region_temp = df_temp['Region'][0]
country_temp = df_temp['country'][0]
ax.scatter(df_temp["Age"], df_temp["Education"])
ax.set_title(f"Region:{region_temp}, Country:{country_temp}")
ax.set_xlabel("Age")
ax.set_ylabel("Education")
plt.tight_layout()
fig.savefig("scatter.pdf")
TA贡献1719条经验 获得超6个赞
在 R 中,您可以这样做:
library(tidyverse)
#get data in list of dataframes
df %>%
select(Region, country, Education, Age) %>%
group_split(Region, country) -> split_data
#From list of data create list of plots.
list_plots <- map(split_data, ~ggplot(.) + aes(Education, Age) +
geom_point() +
ggtitle(sprintf('Plot for region %s and country %s',
first(.$Region), first(.$country))))
#Write the plots in pdf as well as write the csvs.
pdf("plots.pdf", onefile = TRUE)
for (i in seq_along(list_plots)) {
write.csv(split_data, sprintf('%s_%s.csv',
split_data[[i]]$Region[1], split_data[[i]]$country[1]), row.names = FALSE)
print(list_plots[[i]])
}
dev.off()
添加回答
举报