pd.read_csv能抽样取数据吗
时间: 2023-07-06 14:06:35 浏览: 51
是的,pandas库中的`pd.read_csv()`函数可以抽样取数据。你可以使用`pd.read_csv()`函数的`nrows`参数指定要读取的行数,或者使用`pd.read_csv()`函数的`skiprows`和`nrows`参数结合使用,跳过一些行并读取指定行数的数据。例如,下面的代码会随机抽取CSV文件中的50%的数据:
```
import pandas as pd
df = pd.read_csv('data.csv', header=0, skiprows=lambda i: i>0 and random.random() > 0.5)
```
其中,`header=0`表示第一行是数据的列名,`skiprows`参数使用了一个lambda函数,该函数跳过第一行(即列名行),并且当随机生成的数大于0.5时跳过该行。这样,`pd.read_csv()`函数只会读取随机选中的50%的数据。
相关问题
def sample(data,typesample): return data[data['type']==typesample].sample(frac=0.8) types = df['type'].unique() samples = [sample(df,typesample) for typesample in types] samples = pd.concat(samples,axis=0) #print(samples) samples.to_csv("type_gs.csv",index=False) dataset = pd.read_csv('type_gs.csv') #将房型放置第一列 cols = list(dataset) print(cols) cols.insert(0, cols.pop(cols.index('type'))) print(cols) data = dataset.loc[:, cols] data.to_csv('type_gs.csv', index=False)是什么意思
这段代码的作用如下:
首先,定义了一个名为`sample`的函数,用于从数据集中按指定类型随机抽取80%的数据。
然后,获取数据集中所有不同的类型,然后分别对每种类型调用`sample`函数,将抽样后的数据保存在`samples`列表中。
接着,将`samples`列表中的所有数据按行合并,得到一个新的数据集,并将其保存为CSV文件`type_gs.csv`。
读取`type_gs.csv`文件,并将“房型”列移动到第一列。
最后,将新的数据集保存为CSV文件`type_gs.csv`,并覆盖原文件。
整个代码的作用是将原始数据集按照不同的类型进行划分,并对每种类型进行随机抽样,最终得到一个新的数据集,其中每种类型的数据比例大致为原数据集中该类型数据的80%。
在以下代码中添加代码,使得保存的折线图更简洁更美观: import pandas as pd import matplotlib.pyplot as plt df=pd.read_csv('movies_revenue_starring_1950_2010.txt',sep=',') df.sort_values('Title',ascending=False,inplace=True) df_new=df[['Title','Revenue']].reset_index(drop=True) df_new=df_new.iloc[::65,:] #(进行位置索引)对title进行抽样 df_renew=df[['Title','Starring Actors Popularity']].reset_index(drop=True) df_renew=df_renew.iloc[::65,:] #可视化电影的revenue的值 plt.plot(df_new['Title'],df_new['Revenue'],color='red') plt.xticks(rotation=45) #x轴旋转45° plt.title('movies Revenue 1950-2010') plt.xlabel('title') plt.ylabel('Revenue') plt.savefig('movies_revenue_1950_2010.png',dpi=400) plt.show() #可视化电影的Starring Actors Popularity值 plt.plot(df_renew['Title'],df_renew['Starring Actors Popularity'],color='green') plt.xticks(rotation=45) plt.title('Starring Actors Popularity 1950-2010') plt.xlabel('title') plt.ylabel('Starring Actors Popularity') plt.savefig('movies_starpopularity_1950_2010.png',dpi=400) plt.show()
你可以尝试以下代码,使得保存的折线图更简洁更美观:
```
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv('movies_revenue_starring_1950_2010.txt',sep=',')
df.sort_values('Title',ascending=False,inplace=True)
df_new=df[['Title','Revenue']].reset_index(drop=True)
df_new=df_new.iloc[::65,:] #(进行位置索引)对title进行抽样
df_renew=df[['Title','Starring Actors Popularity']].reset_index(drop=True)
df_renew=df_renew.iloc[::65,:]
#可视化电影的revenue的值
plt.plot(df_new['Title'],df_new['Revenue'],color='red')
plt.xticks(rotation=45) #x轴旋转45°
plt.title('movies Revenue 1950-2010')
plt.xlabel('title')
plt.ylabel('Revenue')
plt.tight_layout() # 自动调整子图间的间距和间隔
plt.savefig('movies_revenue_1950_2010.png',dpi=400)
plt.show()
#可视化电影的Starring Actors Popularity值
plt.plot(df_renew['Title'],df_renew['Starring Actors Popularity'],color='green')
plt.xticks(rotation=45)
plt.title('Starring Actors Popularity 1950-2010')
plt.xlabel('title')
plt.ylabel('Starring Actors Popularity')
plt.tight_layout() # 自动调整子图间的间距和间隔
plt.savefig('movies_starpopularity_1950_2010.png',dpi=400)
plt.show()
```
使用 `plt.tight_layout()`函数可以自动调整子图间的间距和间隔,使得保存的折线图更加紧凑和美观。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)