#df
index a b c
1 2 3 4
2 3 4 5
Please help me how to extract columns "a" and "c" with all the rows but without the index column.
df[["a","c"]] # But index no. is also coming, so how to remove the index no.?
DataFrames and Series will always have an index, you can use:
df[["a","c"]].values
output:
array([[2, 4],
[3, 5]], dtype=int64)
numpy.savetxt("foo.csv", a, delimiter=",") where a = df[["a","c"]].valuesThis might work for your case:
# import pandas package as pd
import pandas as pd
# Define a dictionary containing students data
data = {'a': [2, 3],
'b': [3, 4],
'c': [4, 5]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['a', 'b','c'])
print("Given Dataframe :\n", df)
print("\nIterating over rows using iloc function :\n")
# iterate through each row and select
# 0th and 2nd index column respectively.
for i in range(len(df)):
print(df.iloc[i, 0], df.iloc[i, 2])
Output:
Given Dataframe :
a b c
0 2 3 4
1 3 4 5
Iterating over rows using iloc function :
2 4
3 5
Tested here:
https://onecompiler.com/python/3zht3ravp
Solution source -
Method 3: Using iloc[] function of the DataFrame. :
https://www.geeksforgeeks.org/different-ways-to-iterate-over-rows-in-pandas-dataframe/
[row_start:row_end , column_start, column_end] where, row_start refers start row with 0 position as index row_end refers last row with n th position as index column_start refers start column with 0 position as index column_end refers last column with n th position as index geeksforgeeks.org/how-to-get-first-column-of-pandas-dataframe
df[["a","c"]].valuesdf.reset_index(drop=True)[["a", "c"]]