<jupyter_start><jupyter_text>Panda: Fundamental DataFrame Operations  I. Select an index or column from a DataFrame<jupyter_code>import pandas as pd
import numpy as np
# Take a DataFrame as input to your DataFrame 
# intialise data of lists.
data = {'A':[1, 4, 7],
        'B':[2, 5, 6],
        'C':[3, 6, 9]}
df = pd.DataFrame(data)
print(df)<jupyter_output>A  B  C
0  1  2  3
1  4  5  6
2  7  6  9<jupyter_text>access the value that is at index 0, in column ‘A’.<jupyter_code># Using `iloc[]`
print(df.iloc[0][0])

# Using `loc[]`
print(df.loc[0]['A'])

# Using `at[]`
print(df.at[0,'A'])

# Using `iat[]`
print(df.iat[0,0])<jupyter_output>1
1
1
1<jupyter_text>Important to remember .loc[] and .iloc[]  select row and column<jupyter_code>print(df)

# Use `iloc[]` to select row `0`
print(df.iloc[0])

# Use `loc[]` to select column `'A'`
print(df.loc[:,'A'])<jupyter_output>A  B  C
0  1  2  3
1  4  5  6
2  7  6  9
A    1
B    2
C    3
Name: 0, dtype: int64
0    1
1    4
2    7
Name: A, dtype: int64<jupyter_text>II. How to add an Index, Row or Column to DataFrame  How to add an index to DF<jupyter_code>print(df)
df.set_index('C')<jupyter_output><empty_output><jupyter_text>How to add rows to a DF.loc[] vs .ilock[] vs .ix[]- .loc[] works on labels of your index. This means that if you give in loc[2], you look for the values of your DataFrame that have an index labeled 2.- .iloc[] works on the positions in your index. This means that if you give in iloc[2], you look for the values of your DataFrame that are at index ’2`.- .ix[] is a more complex case: when the index is integer-based, you pass a label to .ix[]. ix[2] then means that you’re looking in your DataFrame for values that have an index labeled 2. This is just like .loc[]! However, if your index is not solely integer-based, ix will work with positions, just like .iloc[]<jupyter_code>df = pd.DataFrame(data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), index= [2, 'A', 4], columns=[48, 49, 50])
print(df)
print(df.loc[2])
print(df.iloc[2])
print(df.ix[2])<jupyter_output>48    7
49    8
50    9
Name: 4, dtype: int64<jupyter_text>Adding a Column to Your DataFrame<jupyter_code>df = pd.DataFrame(data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['A', 'B', 'C'])
print(df)
#Use .index
#df['D'] = df.index   # add index to column D
print(df)
# Study the DataFrame `df`
print(df)

# Append a column to `df`
df.loc[:, 4] = pd.Series(['5', '6'], index=df.index)

# Print out `df` again to see the changes
print(df)<jupyter_output>A  B  C  D
0  1  2  3  0
1  4  5  6  1
2  7  8  9  2<jupyter_text>Resetting the Index of Your DataFrame<jupyter_code># Check out the weird index of your dataframe
print(df)

# Use `reset_index()` to reset the values
df_reset = df.reset_index(level=0, drop=True)

# Print `df_reset`
print(df_reset)<jupyter_output>A  B  C  D
0  1  2  3  0
1  4  5  6  1
2  7  8  9  2
   A  B  C  D
0  1  2  3  0
1  4  5  6  1
2  7  8  9  2
{'fraction_non_alphanumeric': 0.1383669285958319, 'fraction_numerical': 0.05329689101469081, 'mean_word_length': 3.5821596244131455, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '414781', 'license': 'MIT', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1204, 'n_words': 444, 'path': 'day5/PandasOperation.ipynb', 'repo_name': 'vsay01/My_AI_Challeges', 'size': '2927'}