Python Pandas "All arrays must be of the same length" Problem
You can try this.
a = {"Ana Linkler":linkler, "İç Linkler":ic_linkler, "Sorular":sorular}
df = pd.DataFrame.from_dict(a, orient='index')
ValueError: arrays must all be same length in python using pandas DataFrame
I'd just construct a Series
for each list and then concat
them all:
In [38]:
l1 = list('abc')
l2 = [1,2,3,4]
s1 = pd.Series(l1, name='list1')
s2 = pd.Series(l2, name='list2')
df = pd.concat([s1,s2], axis=1)
df
Out[38]:
list1 list2
0 a 1
1 b 2
2 c 3
3 NaN 4
As you can pass a name
arg for the Series
ctor it will name each column in the df, plus it will place NaN
where the column lengths don't match
resample
refers to when you have a DatetimeIndex
for which you want to rebase or adjust the length based on some time period which is not what you want here. You want to reindex
which I think is unnecessary and messy:
In [40]:
l1 = list('abc')
l2 = [1,2,3,4]
s1 = pd.Series(l1)
s2 = pd.Series(l2)
df = pd.DataFrame({'list1':s1.reindex(s2.index), 'list2':s2})
df
Out[40]:
list1 list2
0 a 1
1 b 2
2 c 3
3 NaN 4
Here you'd need to know the longest length and then reindex
all Series using that index, if you just concat
it will automatically adjust the lengths and fill missing elements with NaN
ValueError: arrays must all be same length while converting dictionary to data frame
Use DataFrame.from_dict
with orient='index'
:
df = pd.DataFrame.from_dict(a, orient='index')
print (df)
0 1 2 3 4 5
var1 LPES A None None None None
var2 F D None None None None
var3 R T EDUCATION A B
var4 HI HI None None None
var5 PP CCM PP None None
var6 C None None None None
If need 2 columns DataFrame use flatten list comprehension for list of tuples:
df = pd.DataFrame([(k, x) for k, v in a.items() for x in v], columns=['a','b'])
print (df)
a b
0 var1 LPES
1 var1 A
2 var2 F
3 var2 D
4 var3 R
5 var3 T
6 var3 EDUCATION
7 var3 A
8 var3 B
9 var3
10 var4 HI
11 var4 HI
12 var4
13 var5 PP
14 var5 CCM
15 var5 PP
16 var5
17 var6 C
18 var6
Pandas and JSON ValueError: arrays must all be same length
You have different lengths if rows so your original code will fail.
Try this:
import json
from pandas.io.json import json_normalize
with open('Lyrics_SteelyDan.json') as json_data:
data = json.load(json_data)
df = pd.DataFrame(data['songs'])
df['lyrics']
Read also this: https://hackersandslackers.com/json-into-pandas-dataframes/
Related Topics
How to Verify If a Button Is Enabled and Disabled in Webdriver Python
How to Check If Numbers Are in a List in Python
Comparing Items in Lists Within Same Indices Python
How to Execute Local Python Scripts in Jenkins Ui
Numpy Array Typeerror: Only Integer Scalar Arrays Can Be Converted to a Scalar Index
Changing Only One Row to Column in Python
Python: Read Text File and Split File into List Variables, With Each Variable Having 4 Lines Each
Pyspark Regexp_Replace With List Elements Are Not Replacing the String
Remove White Space from Entire Dataframe
How to Run External Executable Using Python
Reduce Multi-Index/Multi-Level Dataframe to Single Index, Single Level
Convert Regular Python String to Raw String
Identifying the Range of a Color in Hsv Using Opencv
How to Remove Name and Dtype from Pandas Output