Error convert Oracle ldr to pandas DataFrame

I’m trying convert a ldr Oracle file into a pandas Dataframe, to get some insights.

I have the following string ‘example’ into my txt file. Where, ‘|’ character represents the field separator and ‘{EOL}’ the delimiter line.

countries.txt (file)

"USA"|"111"| {EOL}"ITALY"|"222"| {EOL}"MEXICO"|"333"|{EOL} 

What I already tried was:

new_element=[] new_element2=[] csvfile = open('d:\countries.txt') for line in csvfile      new_lines = line.split('|{EOL}') for i in new_lines      new_element.append(i) for j in new_element:     new_element2.append(j.replace('"','\'').replace('|',',')) del(new_element2[3])  

After executing the previous commands, we got it:

new_element2 ["'USA','111'", "'ITALY','222'", "'MEXICO','333'"] 

As next step, we are trying convert this "list" to a DataFrame, using the following command:

pd_element = pd.DataFrame(new_element2,columns=['a','b']) 

but, in this moment an error message appears to us:

--------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in create_block_manager_from_blocks(blocks, axes)    1650                 blocks = [make_block(values=blocks[0], -> 1651                                      placement=slice(0, len(axes[0])))]    1652   D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in make_block(values, placement, klass, ndim, dtype, fastpath)    3094  -> 3095     return klass(values, ndim=ndim, placement=placement)    3096   D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim)    2630         super(ObjectBlock, self).__init__(values, ndim=ndim, -> 2631                                           placement=placement)    2632   D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim)      86                 'Wrong number of items passed {val}, placement implies ' ---> 87                 '{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs)))      88   ValueError: Wrong number of items passed 1, placement implies 2  During handling of the above exception, another exception occurred:  ValueError                                Traceback (most recent call last) <ipython-input-446-cec97633cd5d> in <module> ----> 1 pd_element = pd.DataFrame(new_outro,columns=['a','b'])  D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)     449                 else:     450                     mgr = init_ndarray(data, index, columns, dtype=dtype, --> 451                                        copy=copy)     452             else:     453                 mgr = init_dict({}, index, columns, dtype=dtype)  D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\construction.py in init_ndarray(values, index, columns, dtype, copy)     165         values = maybe_infer_to_datetimelike(values)     166  --> 167     return create_block_manager_from_blocks([values], [columns, index])     168      169   D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in create_block_manager_from_blocks(blocks, axes)    1658         blocks = [getattr(b, 'values', b) for b in blocks]    1659         tot_items = sum(b.shape[0] for b in blocks) -> 1660         construction_error(tot_items, blocks[0].shape[1:], axes, e)    1661     1662   D:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in construction_error(tot_items, block_shape, axes, e)    1689         raise ValueError("Empty data passed with indices specified.")    1690     raise ValueError("Shape of passed values is {0}, indices imply {1}".format( -> 1691         passed, implied))    1692     1693   ValueError: Shape of passed values is (3, 1), indices imply (3, 2) 

After some searches, we found this tutorial explaining, we bealive, the same problem that we have and, the solution about our problem: https://datatofish.com/list-to-dataframe/

from pandas import DataFrame      People_List = [['Jon','Smith',21],['Mark','Brown',38],['Maria','Lee',42],['Jill','Jones',28],['Jack','Ford',55]]          df = DataFrame (People_List,columns=['First_Name','Last_Name','Age'])     print (df) 

It works fine!!!

We are observing that the problem is the same: We have a list and wish convert then, into a DataFrame. We understood that part of our problem happening during ldr convert file to list but, unfortunately, we cannot understand "why".

Dears, if someone can pass to us a site whit kind of manual that enable us improve our understand about what is happening, we give you thanks

Download script demo [LINK]
Download script demo [LINK 2]
Download script demo [LINK 2]
Professor Asked on October 22, 2020 in Android langue,   Python.
Add Comment
1 Answer(s)

There are many ways to create a data frame from the list.

1) Basic method: This is the simplest method to create the data frames from the list.

For example:

# import pandas as pd
import pandas as pd
# list of strings
lst = ['apple', 'grape', 'orange', 'mango']
# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
df

2) Using zip() function: We can create the data frame from zipping two lists.

For example:

# import pandas as pd
import pandas as pd
# list of strings
lst1 = ['apple', 'grape', 'orange', 'mango']
# list of int
lst2 = [11, 22, 33, 44]
# Calling DataFrame after zipping both lists, with columns specified
df = pd.DataFrame(list(zip(lst1, lst2)),
columns =['Name', 'value'])
df

Source: List to Dataframe Python

There are many ways to create a data frame from the list.

1) Basic method: This is the simplest method to create the data frames from the list.

For example:

# import pandas as pd
import pandas as pd
# list of strings
lst = ['apple', 'grape', 'orange', 'mango']
# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
df

2) Using zip() function: We can create the data frame from zipping two lists.

For example:

# import pandas as pd
import pandas as pd
# list of strings
lst1 = ['apple', 'grape', 'orange', 'mango']
# list of int
lst2 = [11, 22, 33, 44]
# Calling DataFrame after zipping both lists, with columns specified
df = pd.DataFrame(list(zip(lst1, lst2)),
columns =['Name', 'value'])
df

Source: List to Dataframe Python

Download demo
Default Answered on March 4, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.