My dataframe has 251 lines and only one column. But I want to split this data into separated columns using pandas, specifically.
The text file that I am using to acquire data looks like this,
But using the following code, it results in:
import pandas as pd
directory = 'A://'
spGlass = 'Glass.txt'
fileAir = os.path.join(directory,spGlass)
dataAir = pd.read_csv(fileAir,skiprows=2)
Question: Is there a way to split these data into different columns as it is presented on text file using Pandas??
Related
so i'm trying to import this csv file and each value is seperated by a comma but how do i make new rows and columns from the imported data?
I tried importing it as normal and printing the data frame in different ways.
try the same with
df = pd.read_csv('file_name.csv', sep = ',')
this might work
I am currently doing one of my final assignment and I have a CSV file with a few columns of different data.
Currently interested in extracting out a single column and converting the individual rows into a txt file.
Here is my code:
import pandas as pd
import csv
df = pd.read_csv("AUS_NZ.csv")
print(df.head(10))
print(df["content"])
num_of_review = len(df["content"])
print(num_of_review)
for i in range (num_of_review):
with open ("{}.txt".format(i),"a", encoding="utf-8") as f:
f.write(df["content"][i])
No issue with extracting out the individual rows. But when I examine the txt files that was extracted and look at the content, I noticed that it copied out the text (which is what I want) but it did so twice (which is not what I want).
Example:
"This is an example of what the dataframe have at that particular column which I want to convert to a txt file."
This is what was copied to the txt file:
"This is an example of what the dataframe have at that particular column which I want to convert to a txt file.This is an example of what the dataframe have at that particular column which I want to convert to a txt file."
Any advise on how to just copy the content once only?
Thanks! While thinking about how to rectify this, I came to the same conclusion as you. I made a switch from "a" to "w" and it solved that issue.
Too used to append so I tried that before I tried write.
The correct code:
import pandas as pd
import csv
df = pd.read_csv("AUS_NZ.csv")
print(df.head(10))
print(df["content"])
num_of_review = len(df["content"])
print(num_of_review)
for i in range (num_of_review):
with open ("{}.txt".format(i),"w", encoding="utf-8") as f:
f.write(df["content"][i])
Why when I am trying to print the following code...
import pandas
import csv
passengersid=pandas.read_csv('test.csv', usecols=['PassengerId'])
print(passengersid)
...I am getting this:
Output
I am trying to get a simple list of values (without indexes of values and not a table) from the first (PassengersID) column in one csv file and then iterate and use it in the other csv file along with other data.
You are reading a data frame with the read_csv command.
col_one_list = passengersid['PassengerId'].tolist()
col_one_arr = passengersid['PassengerId'].to_numpy()
this will give you a list or an array as you need
I'm using a simple code to import an Excel file. However, the command is combining the first two rows into one. I would like to keep it separated (as it is in the Excel file).
db=pd.read_excel('fileaddress', sheetname='Sheet1')
I have a question with writing a dataframe format to R.
I have 1000 column X 77 row data. I want to write this dataframe to R data.
When I use function of
r_dataframe = com.convert_to_r_dataframe(df)
it gives me an error like dataframe object has no arttribute type.
When I see the code of com.convert_to_r_dataframe(). it just get the column of dataframe, and get the colunm.dtype.type.
In this moment, the column is dataframe, I think large columns dataframe has inside dataframes?
Any one have some idea to solve this problem?
The data.frame transfer from Python to R could be accomplished with the feather format. Via this link you can find more information.
Quick example.
Export in Python:
import feather
path = 'my_data.feather'
feather.write_dataframe(df, path)
Import in R:
library(feather)
path <- "my_data.feather"
df <- read_feather(path)
In this case you'll have the data in R as a data.frame. You can then decide to write it to an RData file.
save(df, file = 'my_data.RData')
simplest, bestest practical solution is to export in csv
import pandas as pd
dataframe.to_csv('mypath/file.csv')
and then read in R using read.csv