Ayten Yesim Semchenko, Ph.D.

Behavioral Statistician/Researcher

Menu
  • About me
  • Academic Publications
  • Education
  • Games
  • Python and Bash Diaries
  • Contact
Menu

Warming up for data analysis

Posted on July 15, 2019July 15, 2019 by Yesim Semchenko

Hello there,

This week, we will warm ourselves up for data analysis!! I will be using the same data frame that I used in the previous blog post.

Let us first see the descriptive statistics such as mean, standard deviation, min and max values. To do that:

import pandas as pd
df = pd.read_csv("/Users/aytensemchenko/PycharmProjects/datapreparation/mydata.csv")
print(df.describe())

Sometimes, when we prepare our data for the analysis, we would need to add a new variable. For instance, let us add the variable “Total”. Total will be the sum of the ratings for the Image 1 and Image 2. (Remember, participants rated the attractiveness of Image 1 and Image 2). To create the variable “Total”:

import pandas as pd
df = pd.read_csv("/Users/aytensemchenko/PycharmProjects/datapreparation/mydata.csv")
df["Total"] = df["Image1"] + df["Image2"]
print(df)

We used “print” to see our new variable in the console. If we want to apply the changes (i.e., having a new variable column “Total”) into our .csv file:

import pandas as pd
df = pd.read_csv("/Users/aytensemchenko/PycharmProjects/datapreparation/mydata.csv")
df["Total"] = df["Image1"] + df["Image2"]
df.to_csv("modified.csv", index=False)

print(df)

So, now we created a new .csv file including the variable “Total”, and named it “modified.csv”. If we want to drop this new variable, then:

import pandas as pd
df = pd.read_csv("/Users/aytensemchenko/PycharmProjects/datapreparation/mydata.csv")
df["Total"] = df["Image1"] + df["Image2"]
df = df.drop(columns=["Total"])
df.to_csv("modified.csv", index=False)
print(df)

Let us assume that we want to filter our data with multiple conditions (e.g., age and gender). For instance, we only want to see 19 year-old-females. And we want to create a new data frame (new–df), including only these data:

import pandas as pd
df = pd.read_csv("/Users/aytensemchenko/PycharmProjects/datapreparation/mydata.csv")
new_df = df.loc[(df["Age"] == 19) & (df["Gender"] == "F")]
print(new_df)

Lastly, when we want to modify the data: For instance, instead of having “F”, we may want to see “Female” in the data frame. To achieve that:

import pandas as pd
df = pd.read_csv("/Users/aytensemchenko/PycharmProjects/datapreparation/mydata.csv")
df.loc[df["Gender"] == "F", "Gender"] = "Female"
print(df)

Cheers!

Category: Uncategorized

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • October 2022 (1)
  • May 2022 (1)
  • April 2022 (1)
  • December 2021 (1)
  • September 2021 (2)
  • December 2020 (1)
  • June 2020 (1)
  • October 2019 (2)
  • August 2019 (1)
  • July 2019 (3)
  • June 2019 (3)
© 2025 Ayten Yesim Semchenko, Ph.D. | Powered by Minimalist Blog WordPress Theme