## DEV Community is a community of 867,901 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. # Manipulation

Let's say you need to see only one column of your dataframe. To see the 'fixed acidity' column of our dataset, you need to write:

``````df['fixed acidity']
`````` If you add a condition to this column, for example, if you want to see the rows that has a fixed acidity higher than 9:

``````df[df['fixed acidity']>9]
`````` Sometimes you might need rows with multiple conditions added to columns:

``````df[(df['fixed acidity']>9) & (df['citric acid']>0.5)]
`````` If you need to find specific columns:

``````df.loc[:,['volatile acidity', 'chlorides']]
`````` You may want to add conditions with them too, for example, you may want to see the 'volatile acidity' and 'chlorides' content of those rows that have a 'fixed acidity' of 9.2:

``````df.loc[df['fixed acidity'] == 9.2, ['fixed acidity','volatile acidity', 'chlorides']]
`````` You can view the rows for specific indices (as discussed in the previous chapter) too, like this:

``````df.loc[0:3, ['volatile acidity', 'chlorides']]
`````` Now, if you want to locate a specific value, for example, the alchohol content of the wine of 0th row:

``````df['alcohol'].loc
``````

and you will get a value of 9.4

You can find locate a row using its index too:

``````df.iloc
`````` Now if you want to pinpoint a value within this, for example, the 1st attribute (volatile acidity in this case) of the 100th row, try:

``````df.iloc
``````

and you will get 0.61 as expected.

You can locate specific consecutive rows and columns using this iloc command, for example, first three columns of 3rd to 7th row:

``````df.iloc[3:8, 0:3]
`````` and non consecutive rows and columns too:

``````df.iloc[[71, 122, 400], [0, 2]]
`````` What if you want to add a new column to your dataframe? Let's add a 'new column' containing the word 'hi' for all rows:

``````df['new column'] = 'hi'
`````` Let's try changing the value of 'new column' of 0th index of the dataframe using iloc from 'hi' to 'bye':

``````df.iloc[0, df.columns.get_loc('new column')]= 'bye'
`````` Now let's try to find the word starts with 'by' (that we just have added) and replace it with 'hello':

``````df['new column'].loc[df['new column'].str.startswith('by')] = 'hello'
`````` You can also replace null values of your data using pandas. We do not have any null values here, so let's introduce a null value first. Let's replace the string 'hello' with null. To do so, we would need the numpy library.

``````import numpy as np
df['new column'].loc[df['new column'].str.startswith('hel')] = np.nan
`````` To check the number of null values, you can use the isna() method like this:

``````df.isna().sum()
`````` This isna() method can also be used to locate the null value like this:

``````pd.isna(df.head())
`````` Let's replace the null value with 'hey'.

``````df.fillna(value='hey', inplace=True)
`````` If you want to drop null values, use the dropna() method.

Now we will try to create a new dataframe using a loop, where one column of the new dataframe would look the same as the 'new column' of our dataframe df.

``````rows = []
for i in range(df.shape):
rows.append(['hi', 'bye'])
df_new = pd.DataFrame(rows, columns=["new column 2", "new column 3"])
df_new.iloc[0, df_new.columns.get_loc('new column 2')]= 'hey'
`````` You can merge these two dataframes using their common attributes:

``````df_merged = df.merge(df_new, left_on='new column', right_on='new column 2')
`````` You can make necessary variations in your merging operations by dropping mismatched attributes, or by using a column with common name and so on.

You can also group your dataframes:

``````df.groupby(['volatile acidity', 'chlorides']).count().head()
`````` You can also group the dataframes using other attributes like sum.
{"ft_lang_label":"__label__en","ft_lang_prob":0.71044415,"math_prob":0.6903,"size":3649,"snap":"2022-27-2022-33","text_gpt3_token_len":950,"char_repetition_ratio":0.14677641,"word_repetition_ratio":0.0,"special_character_ratio":0.27898055,"punctuation_ratio":0.15769231,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9954009,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40],"im_url_duplicate_count":[null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-02T17:49:02Z\",\"WARC-Record-ID\":\"<urn:uuid:707e2547-a084-4d94-abf1-b636bfdbab58>\",\"Content-Length\":\"140504\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eb4dd6a6-1f1b-4cf1-ac77-138dd2d955c9>\",\"WARC-Concurrent-To\":\"<urn:uuid:77f65c1b-ea26-4ab5-86b5-58cede86091e>\",\"WARC-IP-Address\":\"151.101.66.217\",\"WARC-Target-URI\":\"https://dev.to/orthymarjan/reading-and-manipulating-the-dataset-with-pandas-2-4b32\",\"WARC-Payload-Digest\":\"sha1:42HHA7GBWJYVKBVDMEEBCK5SCZVYFL3G\",\"WARC-Block-Digest\":\"sha1:LJBNVXJAIBTJJOI6NYOVBZAKD2EOVFK2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104189587.61_warc_CC-MAIN-20220702162147-20220702192147-00294.warc.gz\"}"}