# I would like to draw the first row of my dataframe using pandas I would like to have a plot for each case in my dataframe. my x-axis is a range between 10 and 500 and my y-axis is the row values of each column for each case (prob-10, prob-20 and so on up to prob-500)

what i really wants is to have a plot for example for case1 x-axis = 10 , 20 ,30 , ..... y-axis = 0,35, 0,24, 0,44 .....

my dataframe looks like this

cases year HH prob-10 prob-20 prob-30
case1 2018 200 0,35 0,24 0,44
case2 2017 299 0,33 0,25 0,33
case3 2019 200 0,45 0,22 0,33

## 3 Answers

Set index to `cases`, this will ease the `df.plot` function later and [`filter`] only the columns starting with `prob-`. Convert the comma used in the original dataframe to dot so that the dataframe can be interpreted as `float` with `astype(float)`. Then, transpose the dataframe to get the y-axis with the row values of each column for each case, plot the chart with `df.plot(kind='bar')`.

``````import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('sample.csv', sep='\s+')

df = df.set_index('cases')
# get only prob-* columns
df = df.filter(regex='prob-*')
# replace original comma with dot
df = df.apply(lambda x: x.str.replace(',', '.'))
df = df.astype(float)
df = df.T

df.plot(kind='bar')
plt.xticks(rotation=45)
plt.show()
`````` • this looks great but can i have it as scatter plot and for each case separately not in the same graph – sbnm cplk May 9 at 17:22

In case that you meant a row for a specific value of case you could try this:

``````import pandas as pd
import matplotlib.pyplot as plt
import logging

df = pd.DataFrame({"cases": ["case1", "case_2", "case_3"],
"year": ["2017", "2018", "2019"],
"HH": [200, 299, 200],
"prob-10": [0.35, 0.33, 0.45],
"prob-20": [0.24, 0.25, 0.22],
"prob-30": [0.44, 0.33, 0.33]})

def plot_row_as_bars(df: pd.DataFrame,
case: str="case1",
exclude_list: list=["cases", "year", "HH"]):
""" Plot row of a Pandas dataframe as bar chart
"""
temp = df.loc[df["cases"] == case]
# Check temporary dataframe
if len(temp) < 0:
logging.warning(f"Case: {case} is not avialable.")
return

# Exclude columns
temp = temp.drop(exclude_list, axis = 1)

x = temp.columns
y = temp.iloc.to_list()

plt.bar(x, y)
plt.grid()
plt.show()

plot_row_as_bars(df)
`````` • what i really wants is to have a plot for example for case1 x-axis = 10 , 20 ,30 , ..... y-axis = 0,35, 0,24, 0,44 ..... – sbnm cplk May 9 at 17:17
• For a scatter plot just use plt.scatter(x, y) instead of plt.bar(x, y) – pabloberm May 9 at 17:28

I'm not certain what you mean by "draw the first row", but if you want to simply save the first row as a variable you could try casting the dataframe to a dictionary like this.

``````row1 = dict(df)
print(row1)
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.78174555,"math_prob":0.98421425,"size":505,"snap":"2021-21-2021-25","text_gpt3_token_len":186,"char_repetition_ratio":0.12974052,"word_repetition_ratio":0.021052632,"special_character_ratio":0.4950495,"punctuation_ratio":0.2013889,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9994278,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-17T21:55:17Z\",\"WARC-Record-ID\":\"<urn:uuid:84fde26f-a66d-4313-b129-5dc61b858f79>\",\"Content-Length\":\"182472\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:967879d5-fd73-497d-b2a3-12e2d50d4607>\",\"WARC-Concurrent-To\":\"<urn:uuid:89226b5e-735a-4b7b-831e-fc924f738fd6>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/67459698/i-would-like-to-draw-the-first-row-of-my-dataframe-using-pandas\",\"WARC-Payload-Digest\":\"sha1:R6C3GIEPNFSV43VOPMIX7ZB2FUFIB6M7\",\"WARC-Block-Digest\":\"sha1:DWKNQTRN2MDW4R7O2QCPFFJH4KAMTMVT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487633444.37_warc_CC-MAIN-20210617192319-20210617222319-00231.warc.gz\"}"}