Académique Documents
Professionnel Documents
Culture Documents
x={
'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'last_name': ['Miller', 'Jacobson', ".", 'Milner', 'Cooze'],
'age': [42, 52, 36, 24, 73],
'preTestScore': [4, 24, 31, ".", "."],
'postTestScore': ["25,000", "94,000", 57, 62, 70]
}
y=pandas.DataFrame(x)
y.to_csv('F:\example.csv',sep=',')
z=pandas.read_csv('F:\example.csv')
a=pandas.DataFrame(z)
y1=pandas.read_csv('F:\example.csv',header=None)
y2=pandas.DataFrame(y1)
print(y2)
Question 5: Read the example.csv and make the index columns as 'First Name� and
'Last Name'
x={
'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'last_name': ['Miller', 'Jacobson', ".", 'Milner', 'Cooze'],
'age': [42, 52, 36, 24, 73],
'preTestScore': [4, 24, 31, ".", "."],
'postTestScore': ["25,000", "94,000", 57, 62, 70]
}
y=pandas.DataFrame(x)
y1=pandas.read_csv('F:\example.csv',usecols=[1,2])
y2=pandas.DataFrame(y1)
print(y2)
5.7: Read the dataframe by skipping first 3 rows and print the data frame
x={
'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'last_name': ['Miller', 'Jacobson', ".", 'Milner', 'Cooze'],
'age': [42, 52, 36, 24, 73],
'preTestScore': [4, 24, 31, ".", "."],
'postTestScore': ["25,000", "94,000", 57, 62, 70]
}
y=pandas.DataFrame(x)
y1=pandas.read_csv('F:\example.csv',skiprows=3)
y2=pandas.DataFrame(y1)
print(y2)
5.8: Load a csv file while interpreting "," in strings around numbers as thousands
seperators. Check the raw data 'postTestScore' column has, as thousands separator.
data=pandas.read_csv('F:\example.csv', index_col=4).filter(regex='\d{4}')
print(data)
import pandas
import numpy
z=pandas.Series(x)
print(z.str.lower())
print(z.str.upper())
for i in range(len(x)):
print ('Length of',z[i],'=',len(z[i]))
import pandas
import numpy
z=pandas.Series(x)
print(z)
for i in range(len(z)):
print(z[i].strip())
for i in range(len(z)):
print(z[i].lstrip())
for i in range(len(z)):
print(z[i].rstrip())
import pandas
import numpy
z=pandas.Series(x)
for i in range(len(z)):
print(z[i],'=',z[i].split('_'))
import pandas
import numpy
z=pandas.Series(x)
for i in range(len(z)):
print(z.str.replace('x|dog','XX-XXX',case=False))
break
6.5: Create a series and remove dollar from the numeric values
'12', '-$10', '$10,000'
import pandas
import numpy
z=pandas.Series(x)
print(z.str.replace('$',''))
pat=r'[a-z]+'
repl=lambda m: m.group(0)[::-1]
s=pd.Series(['india 1998', 'big country', np.nan]).str.replace(pat,repl)
print s
6.7: Create pandas series and print true if value is alphanumeric in series or
false if
value is not alpha numeric in series.
'1', '2', '1a', '2b', '2003c'
6.8: Create pandas series and print true if value is containing �A�
'1', '2', '1a', '2b', 'America', 'VietnAm','vietnam', '2003c'
print (s.str.contains('A'))
6.10: Create pandas dataframe having keys and ltable and rtable as below -
'key': ['One', 'Two'], 'ltable': [1, 2]
'key': ['One', 'Two'], 'rtable': [4, 5]
y=pandas.DataFrame(ltable)
z=pandas.DataFrame(rtable)
data=y.merge(z)
print(data)