简体   繁体   中英

Iterate through columns in a dataframe and plot on the same graph (Python)

I am relatively new to Python, although I have some basic understanding of data analysis and statistics from my experience as a molecular biologist. With that, I am trying to use Python to plot several columns I have in my dataframe into one nice graph to be able to compare the individual data from various experimental conditions. I have attached an example in a screenshot showing how I have some columns after I did some very basic data wrangling and cleaning (transpose, removing NaN or '0' values, etc.). As you can see, I'd like to be able to loop through columns (let's say columns 2-10 for example) and plot only that data into the kde graph. How can I go about doing this efficiently? Thank you so much for your patience and help! enter image description here

sns.set_style("darkgrid")
sns.kdeplot(data=df['N01_POOL4_1143_totNG_120_sampNG'],shade=True)
sns.kdeplot(data=df['N01_POOL9_2417_totNG_636_sampNG'],shade=True)
sns.kdeplot(data=df['N15_POOL6_1889_totNG_60_sampNG'],shade=True)
sns.kdeplot(data=df['N95_POOL2_669_totNG_75_sampNG'],shade=True)
sns.kdeplot(data=df['PosC1U_POOL2_669_totNG_171_sampNG'],shade=True)
plt.xlim(-400,3000)
plt.ylim(-0.00001,0.004)

You can create a list slice and iterate over it:

for col_name in df.columns.values[1:10]:
  sns.kdeplot(data=df[col_name],shade=True)

Here df1.columns.values returns dataframe column names as list of strings and [1:10] takes a sublist from first to tenth element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM