0% found this document useful (0 votes)
10 views

Python Pandas II

Exercises

Uploaded by

togissovakerke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Pandas II

Exercises

Uploaded by

togissovakerke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Pandas II|Үлкен деректерді өңдеу курсы

DataFrame жаңа жазба (new row) қосу

Example 1 [DataFrame.loc]
import pandas as pd
from numpy.random import randint

dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],


'Maths':[87, 91, 97, 95],
'Science':[83, 99, 84, 76]
}

df = pd.DataFrame(dict)

display(df)

df.loc[len(df.index)] = ['Amy', 89, 93]


display(df)

Example 2.0 [DataFrame.append()]


#from IPython.display import display, HTML

import pandas as pd
import numpy as np

dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],


'Maths':[87, 91, 97, 95],
'Science':[83, 99, 84, 76]
}

df = pd.DataFrame(dict)

display(df)

df2 = {'Name': 'Amy', 'Maths': 89, 'Science': 93}


df = df.append(df2, ignore_index = True)

display(df)

Сұрақтарыңызды WhatsApp 8 708 774 8013 нөмеріне жолдаңыздар.


Python Pandas II|Үлкен деректерді өңдеу курсы

Example 2.1 [DataFrame.append()]


# Importing pandas as pd
import pandas as pd

# Creating the first Dataframe using dictionary


df1 = df = pd.DataFrame({"a":[1, 2, 3, 4],
"b":[5, 6, 7, 8]})

# Creating the Second Dataframe using dictionary


df2 = pd.DataFrame({"a":[1, 2, 3],
"b":[5, 6, 7]})

# Print df1
print(df1, "\n")

# Print df2
df2

df1.append(df2)
df1

Example 2.1 [DataFrame.append() | Append dataframe of different shape]


# Importing pandas as pd
import pandas as pd

# Creating the first Dataframe using dictionary


df1 = pd.DataFrame({"a":[1, 2, 3, 4],
"b":[5, 6, 7, 8]})

# Creating the Second Dataframe using dictionary


df2 = pd.DataFrame({"a":[1, 2, 3],
"b":[5, 6, 7],
"c":[1, 5, 4]})

# for appending df2 at the end of df1


df1.append(df2, ignore_index = True)

Сұрақтарыңызды WhatsApp 8 708 774 8013 нөмеріне жолдаңыздар.


Python Pandas II|Үлкен деректерді өңдеу курсы

Example 3 [pandas.concat()]
#from IPython.display import display, HTML

import pandas as pd
import numpy as np

dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],


'Maths':[87, 91, 97, 95],
'Science':[83, 99, 84, 76]
}

df1 = pd.DataFrame(dict)
display(df1)

dict = {'Name':['Amy', 'Maddy'],


'Maths':[89, 90],
'Science':[93, 81]
}

df2 = pd.DataFrame(dict)
display(df2)

df3 = pd.concat([df1, df2], ignore_index = True)


df3.reset_index()
display(df3)

Example 4
import pandas as pd

data = {'name': ['Somu', 'Kiku', 'Amol', 'Lini'],


'physics': [68, 74, 77, 78],
'chemistry': [84, 56, 73, 69],
'algebra': [78, 88, 82, 87]}

#create dataframe
df_marks = pd.DataFrame(data)
print('Original DataFrame\n------------------')
print(df_marks)

new_row = {'name':'Geo', 'physics':87, 'chemistry':92, 'algebra':97}


#append row to the dataframe
df_marks = df_marks.append(new_row, ignore_index=True)

print('\n\nNew row added to DataFrame\n--------------------------')


print(df_marks)

https://pythonexamples.org/pandas-dataframe-add-append-row/#3
https://www.geeksforgeeks.org/how-to-add-one-row-in-an-existing-pandas-
dataframe/
https://www.geeksforgeeks.org/python-pandas-dataframe-append/

Сұрақтарыңызды WhatsApp 8 708 774 8013 нөмеріне жолдаңыздар.


Python Pandas II|Үлкен деректерді өңдеу курсы

Сұрақтарыңызды WhatsApp 8 708 774 8013 нөмеріне жолдаңыздар.


Python Pandas II|Үлкен деректерді өңдеу курсы

DataFrame жаңа өріс (new column) қосу


Example 1
>>> import pandas as pd

>>> data = {'Name': ['Lia', 'Anna', 'Teo', 'Justin'],


'Height': [171, 162, 181, 192],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
>>>df = pd.DataFrame(data)
>>>address = ['Tokyo', 'Beijing', 'Moscow', 'Almaty']
>>>df['Address'] = address
>>>df

Example 2
>>> import pandas as pd
>>> data = {'Name': ['Lia', 'Anna', 'Teo', 'Justin'],
'Height': [171, 162, 181, 192],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
>>>df = pd.DataFrame(data)
>>>df.insert(2, "Age", [21, 23, 24, 21], True)
>>>df

DataFrame өрісті өшіру (delete column)


Data.csv төмендегі сілтемеден көшіріп алыңыз.
https://drive.google.com/file/d/19zDPwgHRB09b1zCu6q61g2GilzKiSePc/view?usp=sharing
Example 3
>>>import pandas as pd

>>>data = pd.read_csv("data.csv", index_col ="Name" )

>>>data.drop(["Avery Bradley", "John Holland", "R.J. Hunter",


"R.J. Hunter"], inplace = True)
>>>data

Example 4
>>>import pandas as pd
>>>data = pd.read_csv("data.csv", index_col ="Name" )
>>>data.drop(["Team", "Weight"], axis = 1, inplace = True)
>>>data
>>>data.to_csv(“new.csv”)

Сұрақтарыңызды WhatsApp 8 708 774 8013 нөмеріне жолдаңыздар.

You might also like