Pandas notes

From DiLab
Revision as of 14:02, 26 October 2017 by Leo (talk | contribs) (Plot options)
Jump to: navigation, search

Using Python Pandas package for data analysis. Leo's notes.

Data manipulation

Import data from a CSV file to a dataframe

df = pd.read_csv("filename.csv")

Filter the data by a field value

df2 = df.loc[df['Tagid'] == "1234"]

Get all unique values (for example, for the Tagid field)

allTags = df.Tagid.unique()

Pivot the table, where the values become columns. For example, to create a plot that uses unique values (of Tagid) as the series.

dfTags = df.pivot(columns='Tagid', values='RSSI')


Plot options

Define the colors of the plot data

df.plot(color="rgbk")

Different plot types (markers)

df.plot(marker='.')

Limit the X axis values:

df.plot(xlim=(0,4000))

Naming the axis

ax = df.plot()
ax.set_ylabel(AntNames[x])

Placement of the legend (Below-left of the plot)

df.plot().legend(loc='upper left', bbox_to_anchor=(0, 0))

Removing the legend

ax = df.plot()
ax.legend_.remove()

Plotting means and std: link

mymean = byTagAnt.RSSI.mean()
mystd =  byTagAnt.RSSI.std()
mymean.plot(kind="bar", yerr=mystd);

Plotting with various parameters:

p = mymean.plot(figsize=(15,5),legend=False,kind="bar",rot=45,color="green",fontsize=16,yerr=mystd);